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.

317 lines
8.5KB

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