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.

353 lines
10KB

  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
  23. * MLP parser
  24. */
  25. #include <stdint.h>
  26. #include "libavutil/crc.h"
  27. #include "get_bits.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 uint64_t mlp_layout[32] = {
  40. AV_CH_LAYOUT_MONO,
  41. AV_CH_LAYOUT_STEREO,
  42. AV_CH_LAYOUT_2_1,
  43. AV_CH_LAYOUT_2_2,
  44. AV_CH_LAYOUT_STEREO|AV_CH_LOW_FREQUENCY,
  45. AV_CH_LAYOUT_2_1|AV_CH_LOW_FREQUENCY,
  46. AV_CH_LAYOUT_2_2|AV_CH_LOW_FREQUENCY,
  47. AV_CH_LAYOUT_SURROUND,
  48. AV_CH_LAYOUT_4POINT0,
  49. AV_CH_LAYOUT_5POINT0,
  50. AV_CH_LAYOUT_SURROUND|AV_CH_LOW_FREQUENCY,
  51. AV_CH_LAYOUT_4POINT0|AV_CH_LOW_FREQUENCY,
  52. AV_CH_LAYOUT_5POINT1,
  53. AV_CH_LAYOUT_4POINT0,
  54. AV_CH_LAYOUT_5POINT0,
  55. AV_CH_LAYOUT_SURROUND|AV_CH_LOW_FREQUENCY,
  56. AV_CH_LAYOUT_4POINT0|AV_CH_LOW_FREQUENCY,
  57. AV_CH_LAYOUT_5POINT1,
  58. AV_CH_LAYOUT_2_2|AV_CH_LOW_FREQUENCY,
  59. AV_CH_LAYOUT_5POINT0,
  60. AV_CH_LAYOUT_5POINT1,
  61. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  62. };
  63. static const uint8_t thd_chancount[13] = {
  64. // LR C LFE LRs LRvh LRc LRrs Cs Ts LRsd LRw Cvh LFE2
  65. 2, 1, 1, 2, 2, 2, 2, 1, 1, 2, 2, 1, 1
  66. };
  67. static const uint64_t thd_layout[13] = {
  68. AV_CH_FRONT_LEFT|AV_CH_FRONT_RIGHT, // LR
  69. AV_CH_FRONT_CENTER, // C
  70. AV_CH_LOW_FREQUENCY, // LFE
  71. AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT, // LRs
  72. AV_CH_TOP_FRONT_LEFT|AV_CH_TOP_FRONT_RIGHT, // LRvh
  73. AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT, // LRc
  74. AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT, // LRrs
  75. AV_CH_BACK_CENTER, // Cs
  76. AV_CH_TOP_BACK_CENTER, // Ts
  77. AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT, // LRsd
  78. AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER, // LRw
  79. AV_CH_TOP_BACK_CENTER, // Cvh
  80. AV_CH_LOW_FREQUENCY // LFE2
  81. };
  82. static int mlp_samplerate(int in)
  83. {
  84. if (in == 0xF)
  85. return 0;
  86. return (in & 8 ? 44100 : 48000) << (in & 7) ;
  87. }
  88. static int truehd_channels(int chanmap)
  89. {
  90. int channels = 0, i;
  91. for (i = 0; i < 13; i++)
  92. channels += thd_chancount[i] * ((chanmap >> i) & 1);
  93. return channels;
  94. }
  95. static int64_t truehd_layout(int chanmap)
  96. {
  97. int layout = 0, i;
  98. for (i = 0; i < 13; i++)
  99. layout |= thd_layout[i] * ((chanmap >> i) & 1);
  100. return layout;
  101. }
  102. /** Read a major sync info header - contains high level information about
  103. * the stream - sample rate, channel arrangement etc. Most of this
  104. * information is not actually necessary for decoding, only for playback.
  105. * gb must be a freshly initialized GetBitContext with no bits read.
  106. */
  107. int ff_mlp_read_major_sync(void *log, MLPHeaderInfo *mh, GetBitContext *gb)
  108. {
  109. int ratebits;
  110. uint16_t checksum;
  111. assert(get_bits_count(gb) == 0);
  112. if (gb->size_in_bits < 28 << 3) {
  113. av_log(log, AV_LOG_ERROR, "packet too short, unable to read major sync\n");
  114. return -1;
  115. }
  116. checksum = ff_mlp_checksum16(gb->buffer, 26);
  117. if (checksum != AV_RL16(gb->buffer+26)) {
  118. av_log(log, AV_LOG_ERROR, "major sync info header checksum error\n");
  119. return -1;
  120. }
  121. if (get_bits_long(gb, 24) != 0xf8726f) /* Sync words */
  122. return -1;
  123. mh->stream_type = get_bits(gb, 8);
  124. if (mh->stream_type == 0xbb) {
  125. mh->group1_bits = mlp_quants[get_bits(gb, 4)];
  126. mh->group2_bits = mlp_quants[get_bits(gb, 4)];
  127. ratebits = get_bits(gb, 4);
  128. mh->group1_samplerate = mlp_samplerate(ratebits);
  129. mh->group2_samplerate = mlp_samplerate(get_bits(gb, 4));
  130. skip_bits(gb, 11);
  131. mh->channels_mlp = get_bits(gb, 5);
  132. } else if (mh->stream_type == 0xba) {
  133. mh->group1_bits = 24; // TODO: Is this information actually conveyed anywhere?
  134. mh->group2_bits = 0;
  135. ratebits = get_bits(gb, 4);
  136. mh->group1_samplerate = mlp_samplerate(ratebits);
  137. mh->group2_samplerate = 0;
  138. skip_bits(gb, 8);
  139. mh->channels_thd_stream1 = get_bits(gb, 5);
  140. skip_bits(gb, 2);
  141. mh->channels_thd_stream2 = get_bits(gb, 13);
  142. } else
  143. return -1;
  144. mh->access_unit_size = 40 << (ratebits & 7);
  145. mh->access_unit_size_pow2 = 64 << (ratebits & 7);
  146. skip_bits_long(gb, 48);
  147. mh->is_vbr = get_bits1(gb);
  148. mh->peak_bitrate = (get_bits(gb, 15) * mh->group1_samplerate + 8) >> 4;
  149. mh->num_substreams = get_bits(gb, 4);
  150. skip_bits_long(gb, 4 + 11 * 8);
  151. return 0;
  152. }
  153. typedef struct MLPParseContext
  154. {
  155. ParseContext pc;
  156. int bytes_left;
  157. int in_sync;
  158. int num_substreams;
  159. } MLPParseContext;
  160. static av_cold int mlp_init(AVCodecParserContext *s)
  161. {
  162. ff_mlp_init_crc();
  163. return 0;
  164. }
  165. static int mlp_parse(AVCodecParserContext *s,
  166. AVCodecContext *avctx,
  167. const uint8_t **poutbuf, int *poutbuf_size,
  168. const uint8_t *buf, int buf_size)
  169. {
  170. MLPParseContext *mp = s->priv_data;
  171. int sync_present;
  172. uint8_t parity_bits;
  173. int next;
  174. int i, p = 0;
  175. *poutbuf_size = 0;
  176. if (buf_size == 0)
  177. return 0;
  178. if (!mp->in_sync) {
  179. // Not in sync - find a major sync header
  180. for (i = 0; i < buf_size; i++) {
  181. mp->pc.state = (mp->pc.state << 8) | buf[i];
  182. if ((mp->pc.state & 0xfffffffe) == 0xf8726fba &&
  183. // ignore if we do not have the data for the start of header
  184. mp->pc.index + i >= 7) {
  185. mp->in_sync = 1;
  186. mp->bytes_left = 0;
  187. break;
  188. }
  189. }
  190. if (!mp->in_sync) {
  191. ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size);
  192. return buf_size;
  193. }
  194. ff_combine_frame(&mp->pc, i - 7, &buf, &buf_size);
  195. return i - 7;
  196. }
  197. if (mp->bytes_left == 0) {
  198. // Find length of this packet
  199. /* Copy overread bytes from last frame into buffer. */
  200. for(; mp->pc.overread>0; mp->pc.overread--) {
  201. mp->pc.buffer[mp->pc.index++]= mp->pc.buffer[mp->pc.overread_index++];
  202. }
  203. if (mp->pc.index + buf_size < 2) {
  204. ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size);
  205. return buf_size;
  206. }
  207. mp->bytes_left = ((mp->pc.index > 0 ? mp->pc.buffer[0] : buf[0]) << 8)
  208. | (mp->pc.index > 1 ? mp->pc.buffer[1] : buf[1-mp->pc.index]);
  209. mp->bytes_left = (mp->bytes_left & 0xfff) * 2;
  210. mp->bytes_left -= mp->pc.index;
  211. }
  212. next = (mp->bytes_left > buf_size) ? END_NOT_FOUND : mp->bytes_left;
  213. if (ff_combine_frame(&mp->pc, next, &buf, &buf_size) < 0) {
  214. mp->bytes_left -= buf_size;
  215. return buf_size;
  216. }
  217. mp->bytes_left = 0;
  218. sync_present = (AV_RB32(buf + 4) & 0xfffffffe) == 0xf8726fba;
  219. if (!sync_present) {
  220. /* The first nibble of a frame is a parity check of the 4-byte
  221. * access unit header and all the 2- or 4-byte substream headers. */
  222. // Only check when this isn't a sync frame - syncs have a checksum.
  223. parity_bits = 0;
  224. for (i = -1; i < mp->num_substreams; i++) {
  225. parity_bits ^= buf[p++];
  226. parity_bits ^= buf[p++];
  227. if (i < 0 || buf[p-2] & 0x80) {
  228. parity_bits ^= buf[p++];
  229. parity_bits ^= buf[p++];
  230. }
  231. }
  232. if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
  233. av_log(avctx, AV_LOG_INFO, "mlpparse: Parity check failed.\n");
  234. goto lost_sync;
  235. }
  236. } else {
  237. GetBitContext gb;
  238. MLPHeaderInfo mh;
  239. init_get_bits(&gb, buf + 4, (buf_size - 4) << 3);
  240. if (ff_mlp_read_major_sync(avctx, &mh, &gb) < 0)
  241. goto lost_sync;
  242. avctx->bits_per_raw_sample = mh.group1_bits;
  243. if (avctx->bits_per_raw_sample > 16)
  244. avctx->sample_fmt = AV_SAMPLE_FMT_S32;
  245. else
  246. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  247. avctx->sample_rate = mh.group1_samplerate;
  248. avctx->frame_size = mh.access_unit_size;
  249. if (mh.stream_type == 0xbb) {
  250. /* MLP stream */
  251. avctx->channels = mlp_channels[mh.channels_mlp];
  252. avctx->channel_layout = mlp_layout[mh.channels_mlp];
  253. } else { /* mh.stream_type == 0xba */
  254. /* TrueHD stream */
  255. if (mh.channels_thd_stream2) {
  256. avctx->channels = truehd_channels(mh.channels_thd_stream2);
  257. avctx->channel_layout = truehd_layout(mh.channels_thd_stream2);
  258. } else {
  259. avctx->channels = truehd_channels(mh.channels_thd_stream1);
  260. avctx->channel_layout = truehd_layout(mh.channels_thd_stream1);
  261. }
  262. }
  263. if (!mh.is_vbr) /* Stream is CBR */
  264. avctx->bit_rate = mh.peak_bitrate;
  265. mp->num_substreams = mh.num_substreams;
  266. }
  267. *poutbuf = buf;
  268. *poutbuf_size = buf_size;
  269. return next;
  270. lost_sync:
  271. mp->in_sync = 0;
  272. return 1;
  273. }
  274. AVCodecParser ff_mlp_parser = {
  275. { CODEC_ID_MLP, CODEC_ID_TRUEHD },
  276. sizeof(MLPParseContext),
  277. mlp_init,
  278. mlp_parse,
  279. ff_parse_close,
  280. };