mlp_parser.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 libavcodec/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 av_cold int mlp_init(AVCodecParserContext *s)
  115. {
  116. ff_mlp_init_crc();
  117. return 0;
  118. }
  119. static int mlp_parse(AVCodecParserContext *s,
  120. AVCodecContext *avctx,
  121. const uint8_t **poutbuf, int *poutbuf_size,
  122. const uint8_t *buf, int buf_size)
  123. {
  124. MLPParseContext *mp = s->priv_data;
  125. int sync_present;
  126. uint8_t parity_bits;
  127. int next;
  128. int i, p = 0;
  129. *poutbuf_size = 0;
  130. if (buf_size == 0)
  131. return 0;
  132. if (!mp->in_sync) {
  133. // Not in sync - find a major sync header
  134. for (i = 0; i < buf_size; i++) {
  135. mp->pc.state = (mp->pc.state << 8) | buf[i];
  136. if ((mp->pc.state & 0xfffffffe) == 0xf8726fba) {
  137. mp->in_sync = 1;
  138. mp->bytes_left = 0;
  139. break;
  140. }
  141. }
  142. if (!mp->in_sync) {
  143. ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size);
  144. return buf_size;
  145. }
  146. ff_combine_frame(&mp->pc, i - 7, &buf, &buf_size);
  147. return i - 7;
  148. }
  149. if (mp->bytes_left == 0) {
  150. // Find length of this packet
  151. /* Copy overread bytes from last frame into buffer. */
  152. for(; mp->pc.overread>0; mp->pc.overread--) {
  153. mp->pc.buffer[mp->pc.index++]= mp->pc.buffer[mp->pc.overread_index++];
  154. }
  155. if (mp->pc.index + buf_size < 2) {
  156. ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size);
  157. return buf_size;
  158. }
  159. mp->bytes_left = ((mp->pc.index > 0 ? mp->pc.buffer[0] : buf[0]) << 8)
  160. | (mp->pc.index > 1 ? mp->pc.buffer[1] : buf[1-mp->pc.index]);
  161. mp->bytes_left = (mp->bytes_left & 0xfff) * 2;
  162. mp->bytes_left -= mp->pc.index;
  163. }
  164. next = (mp->bytes_left > buf_size) ? END_NOT_FOUND : mp->bytes_left;
  165. if (ff_combine_frame(&mp->pc, next, &buf, &buf_size) < 0) {
  166. mp->bytes_left -= buf_size;
  167. return buf_size;
  168. }
  169. mp->bytes_left = 0;
  170. sync_present = (AV_RB32(buf + 4) & 0xfffffffe) == 0xf8726fba;
  171. if (!sync_present) {
  172. /* The first nibble of a frame is a parity check of the 4-byte
  173. * access unit header and all the 2- or 4-byte substream headers. */
  174. // Only check when this isn't a sync frame - syncs have a checksum.
  175. parity_bits = 0;
  176. for (i = -1; i < mp->num_substreams; i++) {
  177. parity_bits ^= buf[p++];
  178. parity_bits ^= buf[p++];
  179. if (i < 0 || buf[p-2] & 0x80) {
  180. parity_bits ^= buf[p++];
  181. parity_bits ^= buf[p++];
  182. }
  183. }
  184. if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
  185. av_log(avctx, AV_LOG_INFO, "mlpparse: Parity check failed.\n");
  186. goto lost_sync;
  187. }
  188. } else {
  189. GetBitContext gb;
  190. MLPHeaderInfo mh;
  191. init_get_bits(&gb, buf + 4, (buf_size - 4) << 3);
  192. if (ff_mlp_read_major_sync(avctx, &mh, &gb) < 0)
  193. goto lost_sync;
  194. avctx->bits_per_raw_sample = mh.group1_bits;
  195. if (avctx->bits_per_raw_sample > 16)
  196. avctx->sample_fmt = SAMPLE_FMT_S32;
  197. else
  198. avctx->sample_fmt = SAMPLE_FMT_S16;
  199. avctx->sample_rate = mh.group1_samplerate;
  200. avctx->frame_size = mh.access_unit_size;
  201. if (mh.stream_type == 0xbb) {
  202. /* MLP stream */
  203. avctx->channels = mlp_channels[mh.channels_mlp];
  204. } else { /* mh.stream_type == 0xba */
  205. /* TrueHD stream */
  206. if (mh.channels_thd_stream2)
  207. avctx->channels = truehd_channels(mh.channels_thd_stream2);
  208. else
  209. avctx->channels = truehd_channels(mh.channels_thd_stream1);
  210. }
  211. if (!mh.is_vbr) /* Stream is CBR */
  212. avctx->bit_rate = mh.peak_bitrate;
  213. mp->num_substreams = mh.num_substreams;
  214. }
  215. *poutbuf = buf;
  216. *poutbuf_size = buf_size;
  217. return next;
  218. lost_sync:
  219. mp->in_sync = 0;
  220. return 1;
  221. }
  222. AVCodecParser mlp_parser = {
  223. { CODEC_ID_MLP },
  224. sizeof(MLPParseContext),
  225. mlp_init,
  226. mlp_parse,
  227. NULL,
  228. };