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.

228 lines
6.4KB

  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 "parser.h"
  21. #if CONFIG_DOLBY_E_PARSER | CONFIG_DOLBY_E_DECODER
  22. #include "get_bits.h"
  23. #include "put_bits.h"
  24. #include "dolby_e_parser.h"
  25. #include "dolby_e_parser_internal.h"
  26. static int skip_input(DBEContext *s, int nb_words)
  27. {
  28. if (nb_words > s->input_size) {
  29. return AVERROR_INVALIDDATA;
  30. }
  31. s->input += nb_words * s->word_bytes;
  32. s->input_size -= nb_words;
  33. return 0;
  34. }
  35. static int parse_key(DBEContext *s)
  36. {
  37. if (s->key_present) {
  38. const uint8_t *key = s->input;
  39. int ret = skip_input(s, 1);
  40. if (ret < 0)
  41. return ret;
  42. return AV_RB24(key) >> 24 - s->word_bits;
  43. }
  44. return 0;
  45. }
  46. static int convert_input(DBEContext *s, int nb_words, int key)
  47. {
  48. const uint8_t *src = s->input;
  49. uint8_t *dst = s->buffer;
  50. PutBitContext pb;
  51. int i;
  52. av_assert0(nb_words <= 1024u);
  53. if (nb_words > s->input_size) {
  54. return AVERROR_INVALIDDATA;
  55. }
  56. switch (s->word_bits) {
  57. case 16:
  58. for (i = 0; i < nb_words; i++, src += 2, dst += 2)
  59. AV_WB16(dst, AV_RB16(src) ^ key);
  60. break;
  61. case 20:
  62. init_put_bits(&pb, s->buffer, sizeof(s->buffer));
  63. for (i = 0; i < nb_words; i++, src += 3)
  64. put_bits(&pb, 20, AV_RB24(src) >> 4 ^ key);
  65. flush_put_bits(&pb);
  66. break;
  67. case 24:
  68. for (i = 0; i < nb_words; i++, src += 3, dst += 3)
  69. AV_WB24(dst, AV_RB24(src) ^ key);
  70. break;
  71. default:
  72. av_assert0(0);
  73. }
  74. return init_get_bits(&s->gb, s->buffer, nb_words * s->word_bits);
  75. }
  76. int ff_dolby_e_parse_init(DBEContext *s, const uint8_t *buf, int buf_size)
  77. {
  78. int hdr;
  79. if (buf_size < 3)
  80. return AVERROR_INVALIDDATA;
  81. hdr = AV_RB24(buf);
  82. if ((hdr & 0xfffffe) == 0x7888e) {
  83. s->word_bits = 24;
  84. } else if ((hdr & 0xffffe0) == 0x788e0) {
  85. s->word_bits = 20;
  86. } else if ((hdr & 0xfffe00) == 0x78e00) {
  87. s->word_bits = 16;
  88. } else {
  89. if (s->avctx)
  90. av_log(s->avctx, AV_LOG_ERROR, "Invalid frame header\n");
  91. return AVERROR_INVALIDDATA;
  92. }
  93. s->word_bytes = s->word_bits + 7 >> 3;
  94. s->input = buf + s->word_bytes;
  95. s->input_size = buf_size / s->word_bytes - 1;
  96. s->key_present = hdr >> 24 - s->word_bits & 1;
  97. return 0;
  98. }
  99. int ff_dolby_e_parse_header(DBEContext *s, DolbyEHeaderInfo *hdr)
  100. {
  101. int i, ret, key, mtd_size;
  102. if ((key = parse_key(s)) < 0)
  103. return key;
  104. if ((ret = convert_input(s, 1, key)) < 0)
  105. return ret;
  106. skip_bits(&s->gb, 4);
  107. mtd_size = get_bits(&s->gb, 10);
  108. if (!mtd_size) {
  109. if (s->avctx)
  110. av_log(s->avctx, AV_LOG_ERROR, "Invalid metadata size\n");
  111. return AVERROR_INVALIDDATA;
  112. }
  113. if ((ret = convert_input(s, mtd_size, key)) < 0)
  114. return ret;
  115. skip_bits(&s->gb, 14);
  116. hdr->prog_conf = get_bits(&s->gb, 6);
  117. if (hdr->prog_conf > MAX_PROG_CONF) {
  118. if (s->avctx)
  119. av_log(s->avctx, AV_LOG_ERROR, "Invalid program configuration\n");
  120. return AVERROR_INVALIDDATA;
  121. }
  122. hdr->nb_channels = nb_channels_tab[hdr->prog_conf];
  123. hdr->nb_programs = nb_programs_tab[hdr->prog_conf];
  124. hdr->fr_code = get_bits(&s->gb, 4);
  125. hdr->fr_code_orig = get_bits(&s->gb, 4);
  126. if (!sample_rate_tab[hdr->fr_code] ||
  127. !sample_rate_tab[hdr->fr_code_orig]) {
  128. if (s->avctx)
  129. av_log(s->avctx, AV_LOG_ERROR, "Invalid frame rate code\n");
  130. return AVERROR_INVALIDDATA;
  131. }
  132. skip_bits_long(&s->gb, 88);
  133. for (i = 0; i < hdr->nb_channels; i++)
  134. hdr->ch_size[i] = get_bits(&s->gb, 10);
  135. hdr->mtd_ext_size = get_bits(&s->gb, 8);
  136. hdr->meter_size = get_bits(&s->gb, 8);
  137. skip_bits_long(&s->gb, 10 * hdr->nb_programs);
  138. for (i = 0; i < hdr->nb_channels; i++) {
  139. hdr->rev_id[i] = get_bits(&s->gb, 4);
  140. skip_bits1(&s->gb);
  141. hdr->begin_gain[i] = get_bits(&s->gb, 10);
  142. hdr->end_gain[i] = get_bits(&s->gb, 10);
  143. }
  144. if (get_bits_left(&s->gb) < 0) {
  145. if (s->avctx)
  146. av_log(s->avctx, AV_LOG_ERROR, "Read past end of metadata\n");
  147. return AVERROR_INVALIDDATA;
  148. }
  149. return skip_input(s, mtd_size + 1);
  150. }
  151. #endif /* CONFIG_DOLBY_E_PARSER | CONFIG_DOLBY_E_DECODER */
  152. #if CONFIG_DOLBY_E_PARSER
  153. static int dolby_e_parse(AVCodecParserContext *s2, AVCodecContext *avctx,
  154. const uint8_t **poutbuf, int *poutbuf_size,
  155. const uint8_t *buf, int buf_size)
  156. {
  157. DBEParseContext *s1 = s2->priv_data;
  158. DBEContext *s = &s1->dectx;
  159. int ret;
  160. if ((ret = ff_dolby_e_parse_init(s, buf, buf_size)) < 0)
  161. goto end;
  162. if ((ret = ff_dolby_e_parse_header(s, &s1->metadata)) < 0)
  163. goto end;
  164. s2->duration = FRAME_SAMPLES;
  165. switch (s1->metadata.nb_channels) {
  166. case 4:
  167. avctx->channel_layout = AV_CH_LAYOUT_4POINT0;
  168. break;
  169. case 6:
  170. avctx->channel_layout = AV_CH_LAYOUT_5POINT1;
  171. break;
  172. case 8:
  173. avctx->channel_layout = AV_CH_LAYOUT_7POINT1;
  174. break;
  175. }
  176. avctx->channels = s1->metadata.nb_channels;
  177. avctx->sample_rate = sample_rate_tab[s1->metadata.fr_code];
  178. avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
  179. end:
  180. /* always return the full packet. this parser isn't doing any splitting or
  181. combining, only packet analysis */
  182. *poutbuf = buf;
  183. *poutbuf_size = buf_size;
  184. return buf_size;
  185. }
  186. AVCodecParser ff_dolby_e_parser = {
  187. .codec_ids = { AV_CODEC_ID_DOLBY_E },
  188. .priv_data_size = sizeof(DBEParseContext),
  189. .parser_parse = dolby_e_parse,
  190. .parser_close = ff_parse_close,
  191. };
  192. #endif /* CONFIG_DOLBY_E_PARSER */