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.

390 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 "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. static const uint64_t mlp_layout[32] = {
  41. AV_CH_LAYOUT_MONO,
  42. AV_CH_LAYOUT_STEREO,
  43. AV_CH_LAYOUT_2_1,
  44. AV_CH_LAYOUT_2_2,
  45. AV_CH_LAYOUT_STEREO|AV_CH_LOW_FREQUENCY,
  46. AV_CH_LAYOUT_2_1|AV_CH_LOW_FREQUENCY,
  47. AV_CH_LAYOUT_2_2|AV_CH_LOW_FREQUENCY,
  48. AV_CH_LAYOUT_SURROUND,
  49. AV_CH_LAYOUT_4POINT0,
  50. AV_CH_LAYOUT_5POINT0,
  51. AV_CH_LAYOUT_SURROUND|AV_CH_LOW_FREQUENCY,
  52. AV_CH_LAYOUT_4POINT0|AV_CH_LOW_FREQUENCY,
  53. AV_CH_LAYOUT_5POINT1,
  54. AV_CH_LAYOUT_4POINT0,
  55. AV_CH_LAYOUT_5POINT0,
  56. AV_CH_LAYOUT_SURROUND|AV_CH_LOW_FREQUENCY,
  57. AV_CH_LAYOUT_4POINT0|AV_CH_LOW_FREQUENCY,
  58. AV_CH_LAYOUT_5POINT1,
  59. AV_CH_LAYOUT_2_2|AV_CH_LOW_FREQUENCY,
  60. AV_CH_LAYOUT_5POINT0,
  61. AV_CH_LAYOUT_5POINT1,
  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_SURROUND_DIRECT_LEFT|AV_CH_SURROUND_DIRECT_RIGHT, // LRsd
  79. AV_CH_WIDE_LEFT|AV_CH_WIDE_RIGHT, // LRw
  80. AV_CH_TOP_FRONT_CENTER, // Cvh
  81. AV_CH_LOW_FREQUENCY_2, // 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. static uint64_t 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, channel_arrangement;
  112. uint16_t checksum;
  113. assert(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. channel_arrangement = get_bits(gb, 5);
  134. mh->channels_mlp = mlp_channels[channel_arrangement];
  135. mh->channel_layout_mlp = mlp_layout[channel_arrangement];
  136. } else if (mh->stream_type == 0xba) {
  137. mh->group1_bits = 24; // TODO: Is this information actually conveyed anywhere?
  138. mh->group2_bits = 0;
  139. ratebits = get_bits(gb, 4);
  140. mh->group1_samplerate = mlp_samplerate(ratebits);
  141. mh->group2_samplerate = 0;
  142. skip_bits(gb, 8);
  143. channel_arrangement = get_bits(gb, 5);
  144. mh->channels_thd_stream1 = truehd_channels(channel_arrangement);
  145. mh->channel_layout_thd_stream1 = truehd_layout(channel_arrangement);
  146. skip_bits(gb, 2);
  147. channel_arrangement = get_bits(gb, 13);
  148. mh->channels_thd_stream2 = truehd_channels(channel_arrangement);
  149. mh->channel_layout_thd_stream2 = truehd_layout(channel_arrangement);
  150. } else
  151. return AVERROR_INVALIDDATA;
  152. mh->access_unit_size = 40 << (ratebits & 7);
  153. mh->access_unit_size_pow2 = 64 << (ratebits & 7);
  154. skip_bits_long(gb, 48);
  155. mh->is_vbr = get_bits1(gb);
  156. mh->peak_bitrate = (get_bits(gb, 15) * mh->group1_samplerate + 8) >> 4;
  157. mh->num_substreams = get_bits(gb, 4);
  158. skip_bits_long(gb, 4 + 11 * 8);
  159. return 0;
  160. }
  161. typedef struct MLPParseContext
  162. {
  163. ParseContext pc;
  164. int bytes_left;
  165. int in_sync;
  166. int num_substreams;
  167. } MLPParseContext;
  168. static av_cold int mlp_init(AVCodecParserContext *s)
  169. {
  170. ff_mlp_init_crc();
  171. return 0;
  172. }
  173. static int mlp_parse(AVCodecParserContext *s,
  174. AVCodecContext *avctx,
  175. const uint8_t **poutbuf, int *poutbuf_size,
  176. const uint8_t *buf, int buf_size)
  177. {
  178. MLPParseContext *mp = s->priv_data;
  179. int sync_present;
  180. uint8_t parity_bits;
  181. int next;
  182. int i, p = 0;
  183. *poutbuf_size = 0;
  184. if (buf_size == 0)
  185. return 0;
  186. if (!mp->in_sync) {
  187. // Not in sync - find a major sync header
  188. for (i = 0; i < buf_size; i++) {
  189. mp->pc.state = (mp->pc.state << 8) | buf[i];
  190. if ((mp->pc.state & 0xfffffffe) == 0xf8726fba &&
  191. // ignore if we do not have the data for the start of header
  192. mp->pc.index + i >= 7) {
  193. mp->in_sync = 1;
  194. mp->bytes_left = 0;
  195. break;
  196. }
  197. }
  198. if (!mp->in_sync) {
  199. ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size);
  200. return buf_size;
  201. }
  202. ff_combine_frame(&mp->pc, i - 7, &buf, &buf_size);
  203. return i - 7;
  204. }
  205. if (mp->bytes_left == 0) {
  206. // Find length of this packet
  207. /* Copy overread bytes from last frame into buffer. */
  208. for(; mp->pc.overread>0; mp->pc.overread--) {
  209. mp->pc.buffer[mp->pc.index++]= mp->pc.buffer[mp->pc.overread_index++];
  210. }
  211. if (mp->pc.index + buf_size < 2) {
  212. ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size);
  213. return buf_size;
  214. }
  215. mp->bytes_left = ((mp->pc.index > 0 ? mp->pc.buffer[0] : buf[0]) << 8)
  216. | (mp->pc.index > 1 ? mp->pc.buffer[1] : buf[1-mp->pc.index]);
  217. mp->bytes_left = (mp->bytes_left & 0xfff) * 2;
  218. mp->bytes_left -= mp->pc.index;
  219. }
  220. next = (mp->bytes_left > buf_size) ? END_NOT_FOUND : mp->bytes_left;
  221. if (ff_combine_frame(&mp->pc, next, &buf, &buf_size) < 0) {
  222. mp->bytes_left -= buf_size;
  223. return buf_size;
  224. }
  225. mp->bytes_left = 0;
  226. sync_present = (AV_RB32(buf + 4) & 0xfffffffe) == 0xf8726fba;
  227. if (!sync_present) {
  228. /* The first nibble of a frame is a parity check of the 4-byte
  229. * access unit header and all the 2- or 4-byte substream headers. */
  230. // Only check when this isn't a sync frame - syncs have a checksum.
  231. parity_bits = 0;
  232. for (i = -1; i < mp->num_substreams; i++) {
  233. parity_bits ^= buf[p++];
  234. parity_bits ^= buf[p++];
  235. if (i < 0 || buf[p-2] & 0x80) {
  236. parity_bits ^= buf[p++];
  237. parity_bits ^= buf[p++];
  238. }
  239. }
  240. if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
  241. av_log(avctx, AV_LOG_INFO, "mlpparse: Parity check failed.\n");
  242. goto lost_sync;
  243. }
  244. } else {
  245. GetBitContext gb;
  246. MLPHeaderInfo mh;
  247. init_get_bits(&gb, buf + 4, (buf_size - 4) << 3);
  248. if (ff_mlp_read_major_sync(avctx, &mh, &gb) < 0)
  249. goto lost_sync;
  250. avctx->bits_per_raw_sample = mh.group1_bits;
  251. if (avctx->bits_per_raw_sample > 16)
  252. avctx->sample_fmt = AV_SAMPLE_FMT_S32;
  253. else
  254. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  255. avctx->sample_rate = mh.group1_samplerate;
  256. s->duration = mh.access_unit_size;
  257. if (mh.stream_type == 0xbb) {
  258. /* MLP stream */
  259. #if FF_API_REQUEST_CHANNELS
  260. if (avctx->request_channels > 0 && avctx->request_channels <= 2 &&
  261. mh.num_substreams > 1) {
  262. avctx->channels = 2;
  263. avctx->channel_layout = AV_CH_LAYOUT_STEREO;
  264. } else
  265. #endif
  266. if (avctx->request_channel_layout == AV_CH_LAYOUT_STEREO &&
  267. mh.num_substreams > 1) {
  268. avctx->channels = 2;
  269. avctx->channel_layout = AV_CH_LAYOUT_STEREO;
  270. } else {
  271. avctx->channels = mh.channels_mlp;
  272. avctx->channel_layout = mh.channel_layout_mlp;
  273. }
  274. } else { /* mh.stream_type == 0xba */
  275. /* TrueHD stream */
  276. #if FF_API_REQUEST_CHANNELS
  277. if (avctx->request_channels > 0 && avctx->request_channels <= 2 &&
  278. mh.num_substreams > 1) {
  279. avctx->channels = 2;
  280. avctx->channel_layout = AV_CH_LAYOUT_STEREO;
  281. } else if (avctx->request_channels > 0 &&
  282. avctx->request_channels <= mh.channels_thd_stream1) {
  283. avctx->channels = mh.channels_thd_stream1;
  284. avctx->channel_layout = mh.channel_layout_thd_stream1;
  285. } else
  286. #endif
  287. if (avctx->request_channel_layout == AV_CH_LAYOUT_STEREO &&
  288. mh.num_substreams > 1) {
  289. avctx->channels = 2;
  290. avctx->channel_layout = AV_CH_LAYOUT_STEREO;
  291. } else if (avctx->request_channel_layout == mh.channel_layout_thd_stream1 ||
  292. !mh.channels_thd_stream2) {
  293. avctx->channels = mh.channels_thd_stream1;
  294. avctx->channel_layout = mh.channel_layout_thd_stream1;
  295. } else {
  296. avctx->channels = mh.channels_thd_stream2;
  297. avctx->channel_layout = mh.channel_layout_thd_stream2;
  298. }
  299. }
  300. if (!mh.is_vbr) /* Stream is CBR */
  301. avctx->bit_rate = mh.peak_bitrate;
  302. mp->num_substreams = mh.num_substreams;
  303. }
  304. *poutbuf = buf;
  305. *poutbuf_size = buf_size;
  306. return next;
  307. lost_sync:
  308. mp->in_sync = 0;
  309. return 1;
  310. }
  311. AVCodecParser ff_mlp_parser = {
  312. .codec_ids = { AV_CODEC_ID_MLP, AV_CODEC_ID_TRUEHD },
  313. .priv_data_size = sizeof(MLPParseContext),
  314. .parser_init = mlp_init,
  315. .parser_parse = mlp_parse,
  316. .parser_close = ff_parse_close,
  317. };