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.

192 lines
5.5KB

  1. /*
  2. * Copyright (c) 2018 Paul B Mahol
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "avcodec.h"
  21. #include "bsf.h"
  22. #include "get_bits.h"
  23. #include "mlp_parse.h"
  24. #include "mlp.h"
  25. typedef struct AccessUnit {
  26. uint8_t bits[4];
  27. uint16_t offset;
  28. uint16_t optional;
  29. } AccessUnit;
  30. typedef struct TrueHDCoreContext {
  31. const AVClass *class;
  32. MLPHeaderInfo hdr;
  33. } TrueHDCoreContext;
  34. static int truehd_core_filter(AVBSFContext *ctx, AVPacket *out)
  35. {
  36. TrueHDCoreContext *s = ctx->priv_data;
  37. GetBitContext gbc;
  38. AccessUnit units[MAX_SUBSTREAMS];
  39. AVPacket *in;
  40. int ret, i, size, last_offset = 0;
  41. int in_size, out_size;
  42. int have_header = 0;
  43. int substream_bits = 0;
  44. int end;
  45. uint16_t dts;
  46. ret = ff_bsf_get_packet(ctx, &in);
  47. if (ret < 0)
  48. return ret;
  49. if (in->size < 4)
  50. goto fail;
  51. ret = init_get_bits(&gbc, in->data, 32);
  52. if (ret < 0)
  53. goto fail;
  54. skip_bits(&gbc, 4);
  55. in_size = get_bits(&gbc, 12) * 2;
  56. if (in_size < 4 || in_size > in->size)
  57. goto fail;
  58. out_size = in_size;
  59. dts = get_bits(&gbc, 16);
  60. ret = init_get_bits8(&gbc, in->data + 4, in->size - 4);
  61. if (ret < 0)
  62. goto fail;
  63. if (show_bits_long(&gbc, 32) == 0xf8726fba) {
  64. if ((ret = ff_mlp_read_major_sync(ctx, &s->hdr, &gbc)) != 0)
  65. goto fail;
  66. have_header = 1;
  67. }
  68. if (s->hdr.num_substreams > MAX_SUBSTREAMS)
  69. goto fail;
  70. for (i = 0; i < s->hdr.num_substreams; i++) {
  71. for (int j = 0; j < 4; j++)
  72. units[i].bits[j] = get_bits1(&gbc);
  73. units[i].offset = get_bits(&gbc, 12) * 2;
  74. if (i < FFMIN(s->hdr.num_substreams, 3)) {
  75. last_offset = units[i].offset;
  76. substream_bits += 16;
  77. }
  78. if (units[i].bits[0]) {
  79. units[i].optional = get_bits(&gbc, 16);
  80. if (i < FFMIN(s->hdr.num_substreams, 3))
  81. substream_bits += 16;
  82. }
  83. }
  84. end = get_bits_count(&gbc);
  85. size = ((end + 7) >> 3) + 4 + last_offset;
  86. if (size >= 0 && size <= in->size)
  87. out_size = size;
  88. if (out_size < in_size) {
  89. int bpos = 0, reduce = (end - have_header * 28 * 8 - substream_bits) >> 4;
  90. uint16_t parity_nibble = 0;
  91. uint16_t auheader;
  92. ret = av_new_packet(out, out_size);
  93. if (ret < 0)
  94. goto fail;
  95. AV_WB16(out->data + 2, dts);
  96. parity_nibble = dts;
  97. out->size -= reduce * 2;
  98. parity_nibble ^= out->size / 2;
  99. if (have_header) {
  100. memcpy(out->data + 4, in->data + 4, 28);
  101. out->data[16 + 4] = (out->data[16 + 4] & 0x0c) | (FFMIN(s->hdr.num_substreams, 3) << 4);
  102. out->data[17 + 4]&= 0x7f;
  103. out->data[25 + 4] = out->data[25 + 4] & 0xfe;
  104. out->data[26 + 4] = 0xff;
  105. out->data[27 + 4] = 0xff;
  106. AV_WL16(out->data + 4 + 26, ff_mlp_checksum16(out->data + 4, 26));
  107. }
  108. for (i = 0; i < FFMIN(s->hdr.num_substreams, 3); i++) {
  109. uint16_t substr_hdr = 0;
  110. substr_hdr |= (units[i].bits[0] << 15);
  111. substr_hdr |= (units[i].bits[1] << 14);
  112. substr_hdr |= (units[i].bits[2] << 13);
  113. substr_hdr |= (units[i].bits[3] << 12);
  114. substr_hdr |= (units[i].offset / 2) & 0x0FFF;
  115. AV_WB16(out->data + have_header * 28 + 4 + bpos, substr_hdr);
  116. parity_nibble ^= out->data[have_header * 28 + 4 + bpos++];
  117. parity_nibble ^= out->data[have_header * 28 + 4 + bpos++];
  118. if (units[i].bits[0]) {
  119. AV_WB16(out->data + have_header * 28 + 4 + bpos, units[i].optional);
  120. parity_nibble ^= out->data[have_header * 28 + 4 + bpos++];
  121. parity_nibble ^= out->data[have_header * 28 + 4 + bpos++];
  122. }
  123. }
  124. parity_nibble ^= parity_nibble >> 8;
  125. parity_nibble ^= parity_nibble >> 4;
  126. parity_nibble &= 0xF;
  127. memcpy(out->data + have_header * 28 + 4 + bpos,
  128. in->data + 4 + (end >> 3),
  129. out_size - (4 + (end >> 3)));
  130. auheader = (parity_nibble ^ 0xF) << 12;
  131. auheader |= (out->size / 2) & 0x0fff;
  132. AV_WB16(out->data, auheader);
  133. ret = av_packet_copy_props(out, in);
  134. } else {
  135. av_packet_move_ref(out, in);
  136. }
  137. fail:
  138. if (ret < 0)
  139. av_packet_unref(out);
  140. av_packet_free(&in);
  141. return ret;
  142. }
  143. static void truehd_core_flush(AVBSFContext *ctx)
  144. {
  145. TrueHDCoreContext *s = ctx->priv_data;
  146. memset(&s->hdr, 0, sizeof(s->hdr));
  147. }
  148. static const enum AVCodecID codec_ids[] = {
  149. AV_CODEC_ID_TRUEHD, AV_CODEC_ID_NONE,
  150. };
  151. const AVBitStreamFilter ff_truehd_core_bsf = {
  152. .name = "truehd_core",
  153. .priv_data_size = sizeof(TrueHDCoreContext),
  154. .filter = truehd_core_filter,
  155. .flush = truehd_core_flush,
  156. .codec_ids = codec_ids,
  157. };