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.

385 lines
12KB

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