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.

395 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 "get_bits.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. /** Read a major sync info header - contains high level information about
  106. * the stream - sample rate, channel arrangement etc. Most of this
  107. * information is not actually necessary for decoding, only for playback.
  108. * gb must be a freshly initialized GetBitContext with no bits read.
  109. */
  110. int ff_mlp_read_major_sync(void *log, MLPHeaderInfo *mh, GetBitContext *gb)
  111. {
  112. int ratebits, channel_arrangement;
  113. uint16_t checksum;
  114. assert(get_bits_count(gb) == 0);
  115. if (gb->size_in_bits < 28 << 3) {
  116. av_log(log, AV_LOG_ERROR, "packet too short, unable to read major sync\n");
  117. return -1;
  118. }
  119. checksum = ff_mlp_checksum16(gb->buffer, 26);
  120. if (checksum != AV_RL16(gb->buffer+26)) {
  121. av_log(log, AV_LOG_ERROR, "major sync info header checksum error\n");
  122. return AVERROR_INVALIDDATA;
  123. }
  124. if (get_bits_long(gb, 24) != 0xf8726f) /* Sync words */
  125. return AVERROR_INVALIDDATA;
  126. mh->stream_type = get_bits(gb, 8);
  127. if (mh->stream_type == 0xbb) {
  128. mh->group1_bits = mlp_quants[get_bits(gb, 4)];
  129. mh->group2_bits = mlp_quants[get_bits(gb, 4)];
  130. ratebits = get_bits(gb, 4);
  131. mh->group1_samplerate = mlp_samplerate(ratebits);
  132. mh->group2_samplerate = mlp_samplerate(get_bits(gb, 4));
  133. skip_bits(gb, 11);
  134. channel_arrangement = get_bits(gb, 5);
  135. mh->channels_mlp = mlp_channels[channel_arrangement];
  136. mh->channel_layout_mlp = mlp_layout[channel_arrangement];
  137. } else if (mh->stream_type == 0xba) {
  138. mh->group1_bits = 24; // TODO: Is this information actually conveyed anywhere?
  139. mh->group2_bits = 0;
  140. ratebits = get_bits(gb, 4);
  141. mh->group1_samplerate = mlp_samplerate(ratebits);
  142. mh->group2_samplerate = 0;
  143. skip_bits(gb, 8);
  144. channel_arrangement = get_bits(gb, 5);
  145. mh->channels_thd_stream1 = truehd_channels(channel_arrangement);
  146. mh->channel_layout_thd_stream1 = truehd_layout(channel_arrangement);
  147. skip_bits(gb, 2);
  148. channel_arrangement = get_bits(gb, 13);
  149. mh->channels_thd_stream2 = truehd_channels(channel_arrangement);
  150. mh->channel_layout_thd_stream2 = truehd_layout(channel_arrangement);
  151. } else
  152. return AVERROR_INVALIDDATA;
  153. mh->access_unit_size = 40 << (ratebits & 7);
  154. mh->access_unit_size_pow2 = 64 << (ratebits & 7);
  155. skip_bits_long(gb, 48);
  156. mh->is_vbr = get_bits1(gb);
  157. mh->peak_bitrate = (get_bits(gb, 15) * mh->group1_samplerate + 8) >> 4;
  158. mh->num_substreams = get_bits(gb, 4);
  159. skip_bits_long(gb, 4 + 11 * 8);
  160. return 0;
  161. }
  162. typedef struct MLPParseContext
  163. {
  164. ParseContext pc;
  165. int bytes_left;
  166. int in_sync;
  167. int num_substreams;
  168. } MLPParseContext;
  169. static av_cold int mlp_init(AVCodecParserContext *s)
  170. {
  171. ff_mlp_init_crc();
  172. return 0;
  173. }
  174. static int mlp_parse(AVCodecParserContext *s,
  175. AVCodecContext *avctx,
  176. const uint8_t **poutbuf, int *poutbuf_size,
  177. const uint8_t *buf, int buf_size)
  178. {
  179. MLPParseContext *mp = s->priv_data;
  180. int sync_present;
  181. uint8_t parity_bits;
  182. int next;
  183. int i, p = 0;
  184. *poutbuf_size = 0;
  185. if (buf_size == 0)
  186. return 0;
  187. if (!mp->in_sync) {
  188. // Not in sync - find a major sync header
  189. for (i = 0; i < buf_size; i++) {
  190. mp->pc.state = (mp->pc.state << 8) | buf[i];
  191. if ((mp->pc.state & 0xfffffffe) == 0xf8726fba &&
  192. // ignore if we do not have the data for the start of header
  193. mp->pc.index + i >= 7) {
  194. mp->in_sync = 1;
  195. mp->bytes_left = 0;
  196. break;
  197. }
  198. }
  199. if (!mp->in_sync) {
  200. ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size);
  201. return buf_size;
  202. }
  203. ff_combine_frame(&mp->pc, i - 7, &buf, &buf_size);
  204. return i - 7;
  205. }
  206. if (mp->bytes_left == 0) {
  207. // Find length of this packet
  208. /* Copy overread bytes from last frame into buffer. */
  209. for(; mp->pc.overread>0; mp->pc.overread--) {
  210. mp->pc.buffer[mp->pc.index++]= mp->pc.buffer[mp->pc.overread_index++];
  211. }
  212. if (mp->pc.index + buf_size < 2) {
  213. ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size);
  214. return buf_size;
  215. }
  216. mp->bytes_left = ((mp->pc.index > 0 ? mp->pc.buffer[0] : buf[0]) << 8)
  217. | (mp->pc.index > 1 ? mp->pc.buffer[1] : buf[1-mp->pc.index]);
  218. mp->bytes_left = (mp->bytes_left & 0xfff) * 2;
  219. mp->bytes_left -= mp->pc.index;
  220. }
  221. next = (mp->bytes_left > buf_size) ? END_NOT_FOUND : mp->bytes_left;
  222. if (ff_combine_frame(&mp->pc, next, &buf, &buf_size) < 0) {
  223. mp->bytes_left -= buf_size;
  224. return buf_size;
  225. }
  226. mp->bytes_left = 0;
  227. sync_present = (AV_RB32(buf + 4) & 0xfffffffe) == 0xf8726fba;
  228. if (!sync_present) {
  229. /* The first nibble of a frame is a parity check of the 4-byte
  230. * access unit header and all the 2- or 4-byte substream headers. */
  231. // Only check when this isn't a sync frame - syncs have a checksum.
  232. parity_bits = 0;
  233. for (i = -1; i < mp->num_substreams; i++) {
  234. parity_bits ^= buf[p++];
  235. parity_bits ^= buf[p++];
  236. if (i < 0 || buf[p-2] & 0x80) {
  237. parity_bits ^= buf[p++];
  238. parity_bits ^= buf[p++];
  239. }
  240. }
  241. if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
  242. av_log(avctx, AV_LOG_INFO, "mlpparse: Parity check failed.\n");
  243. goto lost_sync;
  244. }
  245. } else {
  246. GetBitContext gb;
  247. MLPHeaderInfo mh;
  248. init_get_bits(&gb, buf + 4, (buf_size - 4) << 3);
  249. if (ff_mlp_read_major_sync(avctx, &mh, &gb) < 0)
  250. goto lost_sync;
  251. avctx->bits_per_raw_sample = mh.group1_bits;
  252. if (avctx->bits_per_raw_sample > 16)
  253. avctx->sample_fmt = AV_SAMPLE_FMT_S32;
  254. else
  255. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  256. avctx->sample_rate = mh.group1_samplerate;
  257. s->duration = mh.access_unit_size;
  258. if (mh.stream_type == 0xbb) {
  259. /* MLP stream */
  260. #if FF_API_REQUEST_CHANNELS
  261. FF_DISABLE_DEPRECATION_WARNINGS
  262. if (avctx->request_channels > 0 && avctx->request_channels <= 2 &&
  263. mh.num_substreams > 1) {
  264. avctx->channels = 2;
  265. avctx->channel_layout = AV_CH_LAYOUT_STEREO;
  266. } else
  267. FF_ENABLE_DEPRECATION_WARNINGS
  268. #endif
  269. if (avctx->request_channel_layout == AV_CH_LAYOUT_STEREO &&
  270. mh.num_substreams > 1) {
  271. avctx->channels = 2;
  272. avctx->channel_layout = AV_CH_LAYOUT_STEREO;
  273. } else {
  274. avctx->channels = mh.channels_mlp;
  275. avctx->channel_layout = mh.channel_layout_mlp;
  276. }
  277. } else { /* mh.stream_type == 0xba */
  278. /* TrueHD stream */
  279. #if FF_API_REQUEST_CHANNELS
  280. FF_DISABLE_DEPRECATION_WARNINGS
  281. if (avctx->request_channels > 0 && avctx->request_channels <= 2 &&
  282. mh.num_substreams > 1) {
  283. avctx->channels = 2;
  284. avctx->channel_layout = AV_CH_LAYOUT_STEREO;
  285. } else if (avctx->request_channels > 0 &&
  286. avctx->request_channels <= mh.channels_thd_stream1) {
  287. avctx->channels = mh.channels_thd_stream1;
  288. avctx->channel_layout = mh.channel_layout_thd_stream1;
  289. } else
  290. FF_ENABLE_DEPRECATION_WARNINGS
  291. #endif
  292. if (avctx->request_channel_layout == AV_CH_LAYOUT_STEREO &&
  293. mh.num_substreams > 1) {
  294. avctx->channels = 2;
  295. avctx->channel_layout = AV_CH_LAYOUT_STEREO;
  296. } else if (avctx->request_channel_layout == mh.channel_layout_thd_stream1 ||
  297. !mh.channels_thd_stream2) {
  298. avctx->channels = mh.channels_thd_stream1;
  299. avctx->channel_layout = mh.channel_layout_thd_stream1;
  300. } else {
  301. avctx->channels = mh.channels_thd_stream2;
  302. avctx->channel_layout = mh.channel_layout_thd_stream2;
  303. }
  304. }
  305. if (!mh.is_vbr) /* Stream is CBR */
  306. avctx->bit_rate = mh.peak_bitrate;
  307. mp->num_substreams = mh.num_substreams;
  308. }
  309. *poutbuf = buf;
  310. *poutbuf_size = buf_size;
  311. return next;
  312. lost_sync:
  313. mp->in_sync = 0;
  314. return 1;
  315. }
  316. AVCodecParser ff_mlp_parser = {
  317. .codec_ids = { AV_CODEC_ID_MLP, AV_CODEC_ID_TRUEHD },
  318. .priv_data_size = sizeof(MLPParseContext),
  319. .parser_init = mlp_init,
  320. .parser_parse = mlp_parse,
  321. .parser_close = ff_parse_close,
  322. };