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.

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