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.

404 lines
13KB

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