8svx.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * 8SVX audio decoder
  3. * Copyright (C) 2008 Jaikrishnan Menon
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file libavcodec/8svx.c
  23. * 8svx audio decoder
  24. * @author Jaikrishnan Menon
  25. * supports: fibonacci delta encoding
  26. * : exponential encoding
  27. */
  28. #include "avcodec.h"
  29. /** decoder context */
  30. typedef struct EightSvxContext {
  31. int16_t fib_acc;
  32. const int16_t *table;
  33. } EightSvxContext;
  34. static const int16_t fibonacci[16] = { -34<<8, -21<<8, -13<<8, -8<<8, -5<<8, -3<<8, -2<<8, -1<<8,
  35. 0, 1<<8, 2<<8, 3<<8, 5<<8, 8<<8, 13<<8, 21<<8 };
  36. static const int16_t exponential[16] = { -128<<8, -64<<8, -32<<8, -16<<8, -8<<8, -4<<8, -2<<8, -1<<8,
  37. 0, 1<<8, 2<<8, 4<<8, 8<<8, 16<<8, 32<<8, 64<<8 };
  38. /** decode a frame */
  39. static int eightsvx_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  40. const uint8_t *buf, int buf_size)
  41. {
  42. EightSvxContext *esc = avctx->priv_data;
  43. int16_t *out_data = data;
  44. int consumed = buf_size;
  45. const uint8_t *buf_end = buf + buf_size;
  46. if((*data_size >> 2) < buf_size)
  47. return -1;
  48. if(avctx->frame_number == 0) {
  49. esc->fib_acc = buf[1] << 8;
  50. buf_size -= 2;
  51. buf += 2;
  52. }
  53. *data_size = buf_size << 2;
  54. while(buf < buf_end) {
  55. uint8_t d = *buf++;
  56. esc->fib_acc += esc->table[d & 0x0f];
  57. *out_data++ = esc->fib_acc;
  58. esc->fib_acc += esc->table[d >> 4];
  59. *out_data++ = esc->fib_acc;
  60. }
  61. return consumed;
  62. }
  63. /** initialize 8svx decoder */
  64. static av_cold int eightsvx_decode_init(AVCodecContext *avctx)
  65. {
  66. EightSvxContext *esc = avctx->priv_data;
  67. switch(avctx->codec->id) {
  68. case CODEC_ID_8SVX_FIB:
  69. esc->table = fibonacci;
  70. break;
  71. case CODEC_ID_8SVX_EXP:
  72. esc->table = exponential;
  73. break;
  74. default:
  75. return -1;
  76. }
  77. avctx->sample_fmt = SAMPLE_FMT_S16;
  78. return 0;
  79. }
  80. AVCodec eightsvx_fib_decoder = {
  81. .name = "8svx_fib",
  82. .type = CODEC_TYPE_AUDIO,
  83. .id = CODEC_ID_8SVX_FIB,
  84. .priv_data_size = sizeof (EightSvxContext),
  85. .init = eightsvx_decode_init,
  86. .decode = eightsvx_decode_frame,
  87. .long_name = NULL_IF_CONFIG_SMALL("8SVX fibonacci"),
  88. };
  89. AVCodec eightsvx_exp_decoder = {
  90. .name = "8svx_exp",
  91. .type = CODEC_TYPE_AUDIO,
  92. .id = CODEC_ID_8SVX_EXP,
  93. .priv_data_size = sizeof (EightSvxContext),
  94. .init = eightsvx_decode_init,
  95. .decode = eightsvx_decode_frame,
  96. .long_name = NULL_IF_CONFIG_SMALL("8SVX exponential"),
  97. };