8svx.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. * 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. AVPacket *avpkt)
  41. {
  42. const uint8_t *buf = avpkt->data;
  43. int buf_size = avpkt->size;
  44. EightSvxContext *esc = avctx->priv_data;
  45. int16_t *out_data = data;
  46. int consumed = buf_size;
  47. const uint8_t *buf_end = buf + buf_size;
  48. if((*data_size >> 2) < buf_size)
  49. return -1;
  50. if(avctx->frame_number == 0) {
  51. esc->fib_acc = buf[1] << 8;
  52. buf_size -= 2;
  53. buf += 2;
  54. }
  55. *data_size = buf_size << 2;
  56. while(buf < buf_end) {
  57. uint8_t d = *buf++;
  58. esc->fib_acc += esc->table[d & 0x0f];
  59. *out_data++ = esc->fib_acc;
  60. esc->fib_acc += esc->table[d >> 4];
  61. *out_data++ = esc->fib_acc;
  62. }
  63. return consumed;
  64. }
  65. /** initialize 8svx decoder */
  66. static av_cold int eightsvx_decode_init(AVCodecContext *avctx)
  67. {
  68. EightSvxContext *esc = avctx->priv_data;
  69. switch(avctx->codec->id) {
  70. case CODEC_ID_8SVX_FIB:
  71. esc->table = fibonacci;
  72. break;
  73. case CODEC_ID_8SVX_EXP:
  74. esc->table = exponential;
  75. break;
  76. default:
  77. return -1;
  78. }
  79. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  80. return 0;
  81. }
  82. AVCodec ff_eightsvx_fib_decoder = {
  83. .name = "8svx_fib",
  84. .type = AVMEDIA_TYPE_AUDIO,
  85. .id = CODEC_ID_8SVX_FIB,
  86. .priv_data_size = sizeof (EightSvxContext),
  87. .init = eightsvx_decode_init,
  88. .decode = eightsvx_decode_frame,
  89. .long_name = NULL_IF_CONFIG_SMALL("8SVX fibonacci"),
  90. };
  91. AVCodec ff_eightsvx_exp_decoder = {
  92. .name = "8svx_exp",
  93. .type = AVMEDIA_TYPE_AUDIO,
  94. .id = CODEC_ID_8SVX_EXP,
  95. .priv_data_size = sizeof (EightSvxContext),
  96. .init = eightsvx_decode_init,
  97. .decode = eightsvx_decode_frame,
  98. .long_name = NULL_IF_CONFIG_SMALL("8SVX exponential"),
  99. };