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.

194 lines
5.6KB

  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 start, 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. start = get_bits_count(&gbc);
  71. for (i = 0; i < s->hdr.num_substreams; i++) {
  72. for (int j = 0; j < 4; j++)
  73. units[i].bits[j] = get_bits1(&gbc);
  74. units[i].offset = get_bits(&gbc, 12) * 2;
  75. if (i < FFMIN(s->hdr.num_substreams, 3)) {
  76. last_offset = units[i].offset;
  77. substream_bits += 16;
  78. }
  79. if (units[i].bits[0]) {
  80. units[i].optional = get_bits(&gbc, 16);
  81. if (i < FFMIN(s->hdr.num_substreams, 3))
  82. substream_bits += 16;
  83. }
  84. }
  85. end = get_bits_count(&gbc);
  86. size = ((end + 7) >> 3) + 4 + last_offset;
  87. if (size >= 0 && size <= in->size)
  88. out_size = size;
  89. if (out_size < in_size) {
  90. int bpos = 0, reduce = (end - start - substream_bits) >> 4;
  91. uint16_t parity_nibble = 0;
  92. uint16_t auheader;
  93. ret = av_new_packet(out, out_size);
  94. if (ret < 0)
  95. goto fail;
  96. AV_WB16(out->data + 2, dts);
  97. parity_nibble = dts;
  98. out->size -= reduce * 2;
  99. parity_nibble ^= out->size / 2;
  100. if (out_size > 8)
  101. AV_WN64(out->data + out_size - 8, 0);
  102. if (have_header) {
  103. memcpy(out->data + 4, in->data + 4, 28);
  104. out->data[16 + 4] = (out->data[16 + 4] & 0x0f) | (FFMIN(s->hdr.num_substreams, 3) << 4);
  105. out->data[25 + 4] = out->data[25 + 4] & 0xfe;
  106. out->data[26 + 4] = 0xff;
  107. out->data[27 + 4] = 0xff;
  108. AV_WL16(out->data + 4 + 26, ff_mlp_checksum16(out->data + 4, 26));
  109. }
  110. for (i = 0; i < FFMIN(s->hdr.num_substreams, 3); i++) {
  111. uint16_t substr_hdr = 0;
  112. substr_hdr |= (units[i].bits[0] << 15);
  113. substr_hdr |= (units[i].bits[1] << 14);
  114. substr_hdr |= (units[i].bits[2] << 13);
  115. substr_hdr |= (units[i].bits[3] << 12);
  116. substr_hdr |= (units[i].offset / 2) & 0x0FFF;
  117. AV_WB16(out->data + have_header * 28 + 4 + bpos, substr_hdr);
  118. parity_nibble ^= out->data[have_header * 28 + 4 + bpos++];
  119. parity_nibble ^= out->data[have_header * 28 + 4 + bpos++];
  120. if (units[i].bits[0]) {
  121. AV_WB16(out->data + have_header * 28 + 4 + bpos, units[i].optional);
  122. parity_nibble ^= out->data[have_header * 28 + 4 + bpos++];
  123. parity_nibble ^= out->data[have_header * 28 + 4 + bpos++];
  124. }
  125. }
  126. parity_nibble ^= parity_nibble >> 8;
  127. parity_nibble ^= parity_nibble >> 4;
  128. parity_nibble &= 0xF;
  129. memcpy(out->data + have_header * 28 + 4 + bpos,
  130. in->data + 4 + (end >> 3),
  131. out_size - (4 + (end >> 3)));
  132. auheader = (parity_nibble ^ 0xF) << 12;
  133. auheader |= (out->size / 2) & 0x0fff;
  134. AV_WB16(out->data, auheader);
  135. ret = av_packet_copy_props(out, in);
  136. } else {
  137. av_packet_move_ref(out, in);
  138. }
  139. fail:
  140. if (ret < 0)
  141. av_packet_unref(out);
  142. av_packet_free(&in);
  143. return ret;
  144. }
  145. static void truehd_core_flush(AVBSFContext *ctx)
  146. {
  147. TrueHDCoreContext *s = ctx->priv_data;
  148. memset(&s->hdr, 0, sizeof(s->hdr));
  149. }
  150. static const enum AVCodecID codec_ids[] = {
  151. AV_CODEC_ID_TRUEHD, AV_CODEC_ID_NONE,
  152. };
  153. const AVBitStreamFilter ff_truehd_core_bsf = {
  154. .name = "truehd_core",
  155. .priv_data_size = sizeof(TrueHDCoreContext),
  156. .filter = truehd_core_filter,
  157. .flush = truehd_core_flush,
  158. .codec_ids = codec_ids,
  159. };