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.

290 lines
7.8KB

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