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.

175 lines
5.1KB

  1. /*
  2. * Copyright (C) 2017 foo86
  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 "get_bits.h"
  21. #include "put_bits.h"
  22. #include "dolby_e.h"
  23. static const uint8_t nb_programs_tab[MAX_PROG_CONF + 1] = {
  24. 2, 3, 2, 3, 4, 5, 4, 5, 6, 7, 8, 1, 2, 3, 3, 4, 5, 6, 1, 2, 3, 4, 1, 1
  25. };
  26. static const uint8_t nb_channels_tab[MAX_PROG_CONF + 1] = {
  27. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 6, 6, 6, 6, 6, 6, 6, 4, 4, 4, 4, 8, 8
  28. };
  29. static int skip_input(DBEContext *s, int nb_words)
  30. {
  31. if (nb_words > s->input_size) {
  32. return AVERROR_INVALIDDATA;
  33. }
  34. s->input += nb_words * s->word_bytes;
  35. s->input_size -= nb_words;
  36. return 0;
  37. }
  38. static int parse_key(DBEContext *s)
  39. {
  40. if (s->key_present) {
  41. const uint8_t *key = s->input;
  42. int ret = skip_input(s, 1);
  43. if (ret < 0)
  44. return ret;
  45. return AV_RB24(key) >> 24 - s->word_bits;
  46. }
  47. return 0;
  48. }
  49. static int convert_input(DBEContext *s, int nb_words, int key)
  50. {
  51. const uint8_t *src = s->input;
  52. uint8_t *dst = s->buffer;
  53. PutBitContext pb;
  54. int i;
  55. av_assert0(nb_words <= 1024u);
  56. if (nb_words > s->input_size) {
  57. return AVERROR_INVALIDDATA;
  58. }
  59. switch (s->word_bits) {
  60. case 16:
  61. for (i = 0; i < nb_words; i++, src += 2, dst += 2)
  62. AV_WB16(dst, AV_RB16(src) ^ key);
  63. break;
  64. case 20:
  65. init_put_bits(&pb, s->buffer, sizeof(s->buffer));
  66. for (i = 0; i < nb_words; i++, src += 3)
  67. put_bits(&pb, 20, AV_RB24(src) >> 4 ^ key);
  68. flush_put_bits(&pb);
  69. break;
  70. case 24:
  71. for (i = 0; i < nb_words; i++, src += 3, dst += 3)
  72. AV_WB24(dst, AV_RB24(src) ^ key);
  73. break;
  74. default:
  75. av_assert0(0);
  76. }
  77. return init_get_bits(&s->gb, s->buffer, nb_words * s->word_bits);
  78. }
  79. int ff_dolby_e_parse_header(DBEContext *s, const uint8_t *buf, int buf_size)
  80. {
  81. DolbyEHeaderInfo *const header = &s->metadata;
  82. int hdr, ret, key, mtd_size;
  83. if (buf_size < 3)
  84. return AVERROR_INVALIDDATA;
  85. hdr = AV_RB24(buf);
  86. if ((hdr & 0xfffffe) == 0x7888e) {
  87. s->word_bits = 24;
  88. } else if ((hdr & 0xffffe0) == 0x788e0) {
  89. s->word_bits = 20;
  90. } else if ((hdr & 0xfffe00) == 0x78e00) {
  91. s->word_bits = 16;
  92. } else {
  93. if (s->avctx)
  94. av_log(s->avctx, AV_LOG_ERROR, "Invalid frame header\n");
  95. return AVERROR_INVALIDDATA;
  96. }
  97. s->word_bytes = s->word_bits + 7 >> 3;
  98. s->input = buf + s->word_bytes;
  99. s->input_size = buf_size / s->word_bytes - 1;
  100. s->key_present = hdr >> 24 - s->word_bits & 1;
  101. if ((key = parse_key(s)) < 0)
  102. return key;
  103. if ((ret = convert_input(s, 1, key)) < 0)
  104. return ret;
  105. skip_bits(&s->gb, 4);
  106. mtd_size = get_bits(&s->gb, 10);
  107. if (!mtd_size) {
  108. if (s->avctx)
  109. av_log(s->avctx, AV_LOG_ERROR, "Invalid metadata size\n");
  110. return AVERROR_INVALIDDATA;
  111. }
  112. if ((ret = convert_input(s, mtd_size, key)) < 0)
  113. return ret;
  114. skip_bits(&s->gb, 14);
  115. header->prog_conf = get_bits(&s->gb, 6);
  116. if (header->prog_conf > MAX_PROG_CONF) {
  117. if (s->avctx)
  118. av_log(s->avctx, AV_LOG_ERROR, "Invalid program configuration\n");
  119. return AVERROR_INVALIDDATA;
  120. }
  121. header->nb_channels = nb_channels_tab[header->prog_conf];
  122. header->nb_programs = nb_programs_tab[header->prog_conf];
  123. header->fr_code = get_bits(&s->gb, 4);
  124. header->fr_code_orig = get_bits(&s->gb, 4);
  125. if (!sample_rate_tab[header->fr_code] ||
  126. !sample_rate_tab[header->fr_code_orig]) {
  127. if (s->avctx)
  128. av_log(s->avctx, AV_LOG_ERROR, "Invalid frame rate code\n");
  129. return AVERROR_INVALIDDATA;
  130. }
  131. skip_bits_long(&s->gb, 88);
  132. for (int i = 0; i < header->nb_channels; i++)
  133. header->ch_size[i] = get_bits(&s->gb, 10);
  134. header->mtd_ext_size = get_bits(&s->gb, 8);
  135. header->meter_size = get_bits(&s->gb, 8);
  136. skip_bits_long(&s->gb, 10 * header->nb_programs);
  137. for (int i = 0; i < header->nb_channels; i++) {
  138. header->rev_id[i] = get_bits(&s->gb, 4);
  139. skip_bits1(&s->gb);
  140. header->begin_gain[i] = get_bits(&s->gb, 10);
  141. header->end_gain[i] = get_bits(&s->gb, 10);
  142. }
  143. if (get_bits_left(&s->gb) < 0) {
  144. if (s->avctx)
  145. av_log(s->avctx, AV_LOG_ERROR, "Read past end of metadata\n");
  146. return AVERROR_INVALIDDATA;
  147. }
  148. return skip_input(s, mtd_size + 1);
  149. }