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.

414 lines
13KB

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