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.

119 lines
3.6KB

  1. /*
  2. * Copyright (C) 2008 Jaikrishnan Menon
  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. /**
  21. * @file
  22. * 8svx audio decoder
  23. * supports: fibonacci delta encoding
  24. * : exponential encoding
  25. *
  26. * For more information about the 8SVX format:
  27. * http://netghost.narod.ru/gff/vendspec/iff/iff.txt
  28. * http://sox.sourceforge.net/AudioFormats-11.html
  29. * http://aminet.net/package/mus/misc/wavepak
  30. * http://amigan.1emu.net/reg/8SVX.txt
  31. *
  32. * Samples can be found here:
  33. * http://aminet.net/mods/smpl/
  34. */
  35. #include "avcodec.h"
  36. /** decoder context */
  37. typedef struct EightSvxContext {
  38. int16_t fib_acc;
  39. const int16_t *table;
  40. } EightSvxContext;
  41. static const int16_t fibonacci[16] = { -34<<8, -21<<8, -13<<8, -8<<8, -5<<8, -3<<8, -2<<8, -1<<8,
  42. 0, 1<<8, 2<<8, 3<<8, 5<<8, 8<<8, 13<<8, 21<<8 };
  43. static const int16_t exponential[16] = { -128<<8, -64<<8, -32<<8, -16<<8, -8<<8, -4<<8, -2<<8, -1<<8,
  44. 0, 1<<8, 2<<8, 4<<8, 8<<8, 16<<8, 32<<8, 64<<8 };
  45. static int eightsvx_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  46. AVPacket *avpkt)
  47. {
  48. const uint8_t *buf = avpkt->data;
  49. int buf_size = avpkt->size;
  50. EightSvxContext *esc = avctx->priv_data;
  51. int16_t *out_data = data;
  52. int consumed = buf_size;
  53. const uint8_t *buf_end = buf + buf_size;
  54. if((*data_size >> 2) < buf_size)
  55. return -1;
  56. if(avctx->frame_number == 0) {
  57. esc->fib_acc = buf[1] << 8;
  58. buf_size -= 2;
  59. buf += 2;
  60. }
  61. *data_size = buf_size << 2;
  62. while(buf < buf_end) {
  63. uint8_t d = *buf++;
  64. esc->fib_acc += esc->table[d & 0x0f];
  65. *out_data++ = esc->fib_acc;
  66. esc->fib_acc += esc->table[d >> 4];
  67. *out_data++ = esc->fib_acc;
  68. }
  69. return consumed;
  70. }
  71. static av_cold int eightsvx_decode_init(AVCodecContext *avctx)
  72. {
  73. EightSvxContext *esc = avctx->priv_data;
  74. switch(avctx->codec->id) {
  75. case CODEC_ID_8SVX_FIB:
  76. esc->table = fibonacci;
  77. break;
  78. case CODEC_ID_8SVX_EXP:
  79. esc->table = exponential;
  80. break;
  81. default:
  82. return -1;
  83. }
  84. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  85. return 0;
  86. }
  87. AVCodec ff_eightsvx_fib_decoder = {
  88. .name = "8svx_fib",
  89. .type = AVMEDIA_TYPE_AUDIO,
  90. .id = CODEC_ID_8SVX_FIB,
  91. .priv_data_size = sizeof (EightSvxContext),
  92. .init = eightsvx_decode_init,
  93. .decode = eightsvx_decode_frame,
  94. .long_name = NULL_IF_CONFIG_SMALL("8SVX fibonacci"),
  95. };
  96. AVCodec ff_eightsvx_exp_decoder = {
  97. .name = "8svx_exp",
  98. .type = AVMEDIA_TYPE_AUDIO,
  99. .id = CODEC_ID_8SVX_EXP,
  100. .priv_data_size = sizeof (EightSvxContext),
  101. .init = eightsvx_decode_init,
  102. .decode = eightsvx_decode_frame,
  103. .long_name = NULL_IF_CONFIG_SMALL("8SVX exponential"),
  104. };