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.

308 lines
8.3KB

  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 "bitstream.h"
  26. #include "parser.h"
  27. #include "crc.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. */
  76. int ff_mlp_read_major_sync(void *log, MLPHeaderInfo *mh, const uint8_t *buf,
  77. unsigned int buf_size)
  78. {
  79. GetBitContext gb;
  80. int ratebits;
  81. uint16_t checksum;
  82. if (buf_size < 28) {
  83. av_log(log, AV_LOG_ERROR, "Packet too short, unable to read major sync\n");
  84. return -1;
  85. }
  86. checksum = mlp_checksum16(buf, 26);
  87. if (checksum != AV_RL16(buf+26)) {
  88. av_log(log, AV_LOG_ERROR, "Major sync info header checksum error\n");
  89. return -1;
  90. }
  91. init_get_bits(&gb, buf, buf_size * 8);
  92. if (get_bits_long(&gb, 24) != 0xf8726f) /* Sync words */
  93. return -1;
  94. mh->stream_type = get_bits(&gb, 8);
  95. if (mh->stream_type == 0xbb) {
  96. mh->group1_bits = mlp_quants[get_bits(&gb, 4)];
  97. mh->group2_bits = mlp_quants[get_bits(&gb, 4)];
  98. ratebits = get_bits(&gb, 4);
  99. mh->group1_samplerate = mlp_samplerate(ratebits);
  100. mh->group2_samplerate = mlp_samplerate(get_bits(&gb, 4));
  101. skip_bits(&gb, 11);
  102. mh->channels_mlp = get_bits(&gb, 5);
  103. } else if (mh->stream_type == 0xba) {
  104. mh->group1_bits = 24; // TODO: Is this information actually conveyed anywhere?
  105. mh->group2_bits = 0;
  106. ratebits = get_bits(&gb, 4);
  107. mh->group1_samplerate = mlp_samplerate(ratebits);
  108. mh->group2_samplerate = 0;
  109. skip_bits(&gb, 8);
  110. mh->channels_thd_stream1 = get_bits(&gb, 5);
  111. skip_bits(&gb, 2);
  112. mh->channels_thd_stream2 = get_bits(&gb, 13);
  113. } else
  114. return -1;
  115. mh->access_unit_size = 40 << (ratebits & 7);
  116. mh->access_unit_size_pow2 = 64 << (ratebits & 7);
  117. skip_bits_long(&gb, 48);
  118. mh->is_vbr = get_bits1(&gb);
  119. mh->peak_bitrate = (get_bits(&gb, 15) * mh->group1_samplerate + 8) >> 4;
  120. mh->num_substreams = get_bits(&gb, 4);
  121. skip_bits_long(&gb, 4 + 11 * 8);
  122. return 0;
  123. }
  124. typedef struct MLPParseContext
  125. {
  126. ParseContext pc;
  127. int bytes_left;
  128. int in_sync;
  129. int num_substreams;
  130. } MLPParseContext;
  131. static int mlp_parse(AVCodecParserContext *s,
  132. AVCodecContext *avctx,
  133. const uint8_t **poutbuf, int *poutbuf_size,
  134. const uint8_t *buf, int buf_size)
  135. {
  136. MLPParseContext *mp = s->priv_data;
  137. int sync_present;
  138. uint8_t parity_bits;
  139. int next;
  140. int i, p = 0;
  141. *poutbuf_size = 0;
  142. if (buf_size == 0)
  143. return 0;
  144. if (!mp->in_sync) {
  145. // Not in sync - find a major sync header
  146. for (i = 0; i < buf_size; i++) {
  147. mp->pc.state = (mp->pc.state << 8) | buf[i];
  148. if ((mp->pc.state & 0xfffffffe) == 0xf8726fba) {
  149. mp->in_sync = 1;
  150. mp->bytes_left = 0;
  151. break;
  152. }
  153. }
  154. if (!mp->in_sync) {
  155. ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size);
  156. return buf_size;
  157. }
  158. ff_combine_frame(&mp->pc, i - 7, &buf, &buf_size);
  159. return i - 7;
  160. }
  161. if (mp->bytes_left == 0) {
  162. // Find length of this packet
  163. /* Copy overread bytes from last frame into buffer. */
  164. for(; mp->pc.overread>0; mp->pc.overread--) {
  165. mp->pc.buffer[mp->pc.index++]= mp->pc.buffer[mp->pc.overread_index++];
  166. }
  167. if (mp->pc.index + buf_size < 2) {
  168. ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size);
  169. return buf_size;
  170. }
  171. mp->bytes_left = ((mp->pc.index > 0 ? mp->pc.buffer[0] : buf[0]) << 8)
  172. | (mp->pc.index > 1 ? mp->pc.buffer[1] : buf[1-mp->pc.index]);
  173. mp->bytes_left = (mp->bytes_left & 0xfff) * 2;
  174. mp->bytes_left -= mp->pc.index;
  175. }
  176. next = (mp->bytes_left > buf_size) ? END_NOT_FOUND : mp->bytes_left;
  177. if (ff_combine_frame(&mp->pc, next, &buf, &buf_size) < 0) {
  178. mp->bytes_left -= buf_size;
  179. return buf_size;
  180. }
  181. mp->bytes_left = 0;
  182. sync_present = (AV_RB32(buf + 4) & 0xfffffffe) == 0xf8726fba;
  183. if (!sync_present) {
  184. // First nibble of a frame is a parity check of the first few nibbles.
  185. // Only check when this isn't a sync frame - syncs have a checksum.
  186. parity_bits = 0;
  187. for (i = 0; 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. MLPHeaderInfo mh;
  201. if (ff_mlp_read_major_sync(avctx, &mh, buf + 4, buf_size - 4) < 0)
  202. goto lost_sync;
  203. #ifdef CONFIG_AUDIO_NONSHORT
  204. avctx->bits_per_sample = mh.group1_bits;
  205. if (avctx->bits_per_sample > 16)
  206. avctx->sample_fmt = SAMPLE_FMT_S32;
  207. #endif
  208. avctx->sample_rate = mh.group1_samplerate;
  209. avctx->frame_size = mh.access_unit_size;
  210. if (mh.stream_type == 0xbb) {
  211. /* MLP stream */
  212. avctx->channels = mlp_channels[mh.channels_mlp];
  213. } else { /* mh.stream_type == 0xba */
  214. /* TrueHD stream */
  215. if (mh.channels_thd_stream2)
  216. avctx->channels = truehd_channels(mh.channels_thd_stream2);
  217. else
  218. avctx->channels = truehd_channels(mh.channels_thd_stream1);
  219. }
  220. if (!mh.is_vbr) /* Stream is CBR */
  221. avctx->bit_rate = mh.peak_bitrate;
  222. mp->num_substreams = mh.num_substreams;
  223. }
  224. *poutbuf = buf;
  225. *poutbuf_size = buf_size;
  226. return next;
  227. lost_sync:
  228. mp->in_sync = 0;
  229. return -1;
  230. }
  231. AVCodecParser mlp_parser = {
  232. { CODEC_ID_MLP },
  233. sizeof(MLPParseContext),
  234. NULL,
  235. mlp_parse,
  236. NULL,
  237. };