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.

115 lines
3.4KB

  1. /*
  2. * 8SVX audio decoder
  3. * Copyright (C) 2008 Jaikrishnan Menon
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * 8svx audio decoder
  24. * @author Jaikrishnan Menon
  25. *
  26. * supports: fibonacci delta encoding
  27. * : exponential encoding
  28. */
  29. #include "avcodec.h"
  30. /** decoder context */
  31. typedef struct EightSvxContext {
  32. int16_t fib_acc;
  33. const int16_t *table;
  34. } EightSvxContext;
  35. static const int16_t fibonacci[16] = { -34<<8, -21<<8, -13<<8, -8<<8, -5<<8, -3<<8, -2<<8, -1<<8,
  36. 0, 1<<8, 2<<8, 3<<8, 5<<8, 8<<8, 13<<8, 21<<8 };
  37. static const int16_t exponential[16] = { -128<<8, -64<<8, -32<<8, -16<<8, -8<<8, -4<<8, -2<<8, -1<<8,
  38. 0, 1<<8, 2<<8, 4<<8, 8<<8, 16<<8, 32<<8, 64<<8 };
  39. /** decode a frame */
  40. static int eightsvx_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  41. AVPacket *avpkt)
  42. {
  43. const uint8_t *buf = avpkt->data;
  44. int buf_size = avpkt->size;
  45. EightSvxContext *esc = avctx->priv_data;
  46. int16_t *out_data = data;
  47. int consumed = buf_size;
  48. const uint8_t *buf_end = buf + buf_size;
  49. if((*data_size >> 2) < buf_size)
  50. return -1;
  51. if(avctx->frame_number == 0) {
  52. esc->fib_acc = buf[1] << 8;
  53. buf_size -= 2;
  54. buf += 2;
  55. }
  56. *data_size = buf_size << 2;
  57. while(buf < buf_end) {
  58. uint8_t d = *buf++;
  59. esc->fib_acc += esc->table[d & 0x0f];
  60. *out_data++ = esc->fib_acc;
  61. esc->fib_acc += esc->table[d >> 4];
  62. *out_data++ = esc->fib_acc;
  63. }
  64. return consumed;
  65. }
  66. /** initialize 8svx decoder */
  67. static av_cold int eightsvx_decode_init(AVCodecContext *avctx)
  68. {
  69. EightSvxContext *esc = avctx->priv_data;
  70. switch(avctx->codec->id) {
  71. case CODEC_ID_8SVX_FIB:
  72. esc->table = fibonacci;
  73. break;
  74. case CODEC_ID_8SVX_EXP:
  75. esc->table = exponential;
  76. break;
  77. default:
  78. return -1;
  79. }
  80. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  81. return 0;
  82. }
  83. AVCodec ff_eightsvx_fib_decoder = {
  84. .name = "8svx_fib",
  85. .type = AVMEDIA_TYPE_AUDIO,
  86. .id = CODEC_ID_8SVX_FIB,
  87. .priv_data_size = sizeof (EightSvxContext),
  88. .init = eightsvx_decode_init,
  89. .decode = eightsvx_decode_frame,
  90. .long_name = NULL_IF_CONFIG_SMALL("8SVX fibonacci"),
  91. };
  92. AVCodec ff_eightsvx_exp_decoder = {
  93. .name = "8svx_exp",
  94. .type = AVMEDIA_TYPE_AUDIO,
  95. .id = CODEC_ID_8SVX_EXP,
  96. .priv_data_size = sizeof (EightSvxContext),
  97. .init = eightsvx_decode_init,
  98. .decode = eightsvx_decode_frame,
  99. .long_name = NULL_IF_CONFIG_SMALL("8SVX exponential"),
  100. };