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.

145 lines
3.8KB

  1. /*
  2. * HCOM audio decoder
  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 "libavutil/intreadwrite.h"
  21. #include "avcodec.h"
  22. #include "get_bits.h"
  23. #include "internal.h"
  24. typedef struct HEntry {
  25. int16_t l, r;
  26. } HEntry;
  27. typedef struct HCOMContext {
  28. AVCodecContext *avctx;
  29. uint8_t first_sample;
  30. uint8_t sample;
  31. int dict_entries;
  32. int dict_entry;
  33. int delta_compression;
  34. HEntry *dict;
  35. } HCOMContext;
  36. static av_cold int hcom_init(AVCodecContext *avctx)
  37. {
  38. HCOMContext *s = avctx->priv_data;
  39. if (avctx->channels != 1) {
  40. av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
  41. return AVERROR_INVALIDDATA;
  42. }
  43. if (avctx->extradata_size <= 7)
  44. return AVERROR_INVALIDDATA;
  45. s->dict_entries = AV_RB16(avctx->extradata);
  46. if (avctx->extradata_size < s->dict_entries * 4 + 7 ||
  47. s->dict_entries == 0)
  48. return AVERROR_INVALIDDATA;
  49. s->delta_compression = AV_RB32(avctx->extradata + 2);
  50. s->sample = s->first_sample = avctx->extradata[avctx->extradata_size - 1];
  51. s->dict = av_calloc(s->dict_entries, sizeof(*s->dict));
  52. if (!s->dict)
  53. return AVERROR(ENOMEM);
  54. for (int i = 0; i < s->dict_entries; i++) {
  55. s->dict[i].l = AV_RB16(avctx->extradata + 6 + 4 * i);
  56. s->dict[i].r = AV_RB16(avctx->extradata + 6 + 4 * i + 2);
  57. if (s->dict[i].l >= 0 &&
  58. (s->dict[i].l >= s->dict_entries ||
  59. s->dict[i].r >= s->dict_entries))
  60. return AVERROR_INVALIDDATA;
  61. }
  62. avctx->sample_fmt = AV_SAMPLE_FMT_U8;
  63. s->dict_entry = 0;
  64. return 0;
  65. }
  66. static int hcom_decode(AVCodecContext *avctx, void *data,
  67. int *got_frame, AVPacket *pkt)
  68. {
  69. HCOMContext *s = avctx->priv_data;
  70. AVFrame *frame = data;
  71. GetBitContext gb;
  72. int ret, n = 0;
  73. if (pkt->size > INT16_MAX)
  74. return AVERROR_INVALIDDATA;
  75. frame->nb_samples = pkt->size * 8;
  76. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  77. return ret;
  78. if ((ret = init_get_bits8(&gb, pkt->data, pkt->size)) < 0)
  79. return ret;
  80. while (get_bits_left(&gb) > 0) {
  81. if (get_bits1(&gb))
  82. s->dict_entry = s->dict[s->dict_entry].r;
  83. else
  84. s->dict_entry = s->dict[s->dict_entry].l;
  85. if (s->dict[s->dict_entry].l < 0) {
  86. int16_t datum;
  87. datum = s->dict[s->dict_entry].r;
  88. if (!s->delta_compression)
  89. s->sample = 0;
  90. s->sample = (s->sample + datum) & 0xFF;
  91. frame->data[0][n++] = s->sample;
  92. s->dict_entry = 0;
  93. }
  94. }
  95. frame->nb_samples = n;
  96. *got_frame = 1;
  97. return pkt->size;
  98. }
  99. static av_cold int hcom_close(AVCodecContext *avctx)
  100. {
  101. HCOMContext *s = avctx->priv_data;
  102. av_freep(&s->dict);
  103. return 0;
  104. }
  105. AVCodec ff_hcom_decoder = {
  106. .name = "hcom",
  107. .long_name = NULL_IF_CONFIG_SMALL("HCOM Audio"),
  108. .type = AVMEDIA_TYPE_AUDIO,
  109. .id = AV_CODEC_ID_HCOM,
  110. .priv_data_size = sizeof(HCOMContext),
  111. .init = hcom_init,
  112. .close = hcom_close,
  113. .decode = hcom_decode,
  114. .capabilities = AV_CODEC_CAP_DR1,
  115. };