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.

232 lines
7.4KB

  1. /*
  2. * Copyright (C) 2008 Jaikrishnan Menon
  3. * Copyright (C) 2011 Stefano Sabatini
  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
  23. * 8svx audio decoder
  24. * @author Jaikrishnan Menon
  25. *
  26. * supports: fibonacci delta encoding
  27. * : exponential encoding
  28. *
  29. * For more information about the 8SVX format:
  30. * http://netghost.narod.ru/gff/vendspec/iff/iff.txt
  31. * http://sox.sourceforge.net/AudioFormats-11.html
  32. * http://aminet.net/package/mus/misc/wavepak
  33. * http://amigan.1emu.net/reg/8SVX.txt
  34. *
  35. * Samples can be found here:
  36. * http://aminet.net/mods/smpl/
  37. */
  38. #include "avcodec.h"
  39. /** decoder context */
  40. typedef struct EightSvxContext {
  41. const int8_t *table;
  42. /* buffer used to store the whole audio decoded/interleaved chunk,
  43. * which is sent with the first packet */
  44. uint8_t *samples;
  45. size_t samples_size;
  46. int samples_idx;
  47. } EightSvxContext;
  48. static const int8_t fibonacci[16] = { -34, -21, -13, -8, -5, -3, -2, -1, 0, 1, 2, 3, 5, 8, 13, 21 };
  49. static const int8_t exponential[16] = { -128, -64, -32, -16, -8, -4, -2, -1, 0, 1, 2, 4, 8, 16, 32, 64 };
  50. #define MAX_FRAME_SIZE 2048
  51. /**
  52. * Interleave samples in buffer containing all left channel samples
  53. * at the beginning, and right channel samples at the end.
  54. * Each sample is assumed to be in signed 8-bit format.
  55. *
  56. * @param size the size in bytes of the dst and src buffer
  57. */
  58. static void interleave_stereo(uint8_t *dst, const uint8_t *src, int size)
  59. {
  60. uint8_t *dst_end = dst + size;
  61. size = size>>1;
  62. while (dst < dst_end) {
  63. *dst++ = *src;
  64. *dst++ = *(src+size);
  65. src++;
  66. }
  67. }
  68. /**
  69. * Delta decode the compressed values in src, and put the resulting
  70. * decoded n samples in dst.
  71. *
  72. * @param val starting value assumed by the delta sequence
  73. * @param table delta sequence table
  74. * @return size in bytes of the decoded data, must be src_size*2
  75. */
  76. static int delta_decode(int8_t *dst, const uint8_t *src, int src_size,
  77. int8_t val, const int8_t *table)
  78. {
  79. int n = src_size;
  80. int8_t *dst0 = dst;
  81. while (n--) {
  82. uint8_t d = *src++;
  83. val = av_clip(val + table[d & 0x0f], -127, 128);
  84. *dst++ = val;
  85. val = av_clip(val + table[d >> 4] , -127, 128);
  86. *dst++ = val;
  87. }
  88. return dst-dst0;
  89. }
  90. static int eightsvx_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  91. AVPacket *avpkt)
  92. {
  93. EightSvxContext *esc = avctx->priv_data;
  94. int out_data_size, n;
  95. uint8_t *src, *dst;
  96. /* decode and interleave the first packet */
  97. if (!esc->samples && avpkt) {
  98. uint8_t *deinterleaved_samples;
  99. esc->samples_size = avctx->codec->id == CODEC_ID_8SVX_RAW ?
  100. avpkt->size : avctx->channels + (avpkt->size-avctx->channels) * 2;
  101. if (!(esc->samples = av_malloc(esc->samples_size)))
  102. return AVERROR(ENOMEM);
  103. /* decompress */
  104. if (avctx->codec->id == CODEC_ID_8SVX_FIB || avctx->codec->id == CODEC_ID_8SVX_EXP) {
  105. const uint8_t *buf = avpkt->data;
  106. int buf_size = avpkt->size;
  107. int n = esc->samples_size;
  108. if (buf_size < 2) {
  109. av_log(avctx, AV_LOG_ERROR, "packet size is too small\n");
  110. return AVERROR(EINVAL);
  111. }
  112. if (!(deinterleaved_samples = av_mallocz(n)))
  113. return AVERROR(ENOMEM);
  114. /* the uncompressed starting value is contained in the first byte */
  115. if (avctx->channels == 2) {
  116. delta_decode(deinterleaved_samples , buf+1, buf_size/2-1, buf[0], esc->table);
  117. buf += buf_size/2;
  118. delta_decode(deinterleaved_samples+n/2-1, buf+1, buf_size/2-1, buf[0], esc->table);
  119. } else
  120. delta_decode(deinterleaved_samples , buf+1, buf_size-1 , buf[0], esc->table);
  121. } else {
  122. deinterleaved_samples = avpkt->data;
  123. }
  124. if (avctx->channels == 2)
  125. interleave_stereo(esc->samples, deinterleaved_samples, esc->samples_size);
  126. else
  127. memcpy(esc->samples, deinterleaved_samples, esc->samples_size);
  128. }
  129. /* return single packed with fixed size */
  130. out_data_size = FFMIN(MAX_FRAME_SIZE, esc->samples_size - esc->samples_idx);
  131. if (*data_size < out_data_size) {
  132. av_log(avctx, AV_LOG_ERROR, "Provided buffer with size %d is too small.\n", *data_size);
  133. return AVERROR(EINVAL);
  134. }
  135. *data_size = out_data_size;
  136. dst = data;
  137. src = esc->samples + esc->samples_idx;
  138. for (n = out_data_size; n > 0; n--)
  139. *dst++ = *src++ + 128;
  140. esc->samples_idx += *data_size;
  141. return avctx->codec->id == CODEC_ID_8SVX_FIB || avctx->codec->id == CODEC_ID_8SVX_EXP ?
  142. (avctx->frame_number == 0)*2 + out_data_size / 2 :
  143. out_data_size;
  144. }
  145. static av_cold int eightsvx_decode_init(AVCodecContext *avctx)
  146. {
  147. EightSvxContext *esc = avctx->priv_data;
  148. if (avctx->channels > 2) {
  149. av_log(avctx, AV_LOG_ERROR, "8SVX does not support more than 2 channels\n");
  150. return AVERROR_INVALIDDATA;
  151. }
  152. switch (avctx->codec->id) {
  153. case CODEC_ID_8SVX_FIB: esc->table = fibonacci; break;
  154. case CODEC_ID_8SVX_EXP: esc->table = exponential; break;
  155. case CODEC_ID_8SVX_RAW: esc->table = NULL; break;
  156. default:
  157. av_log(avctx, AV_LOG_ERROR, "Invalid codec id %d.\n", avctx->codec->id);
  158. return AVERROR_INVALIDDATA;
  159. }
  160. avctx->sample_fmt = AV_SAMPLE_FMT_U8;
  161. return 0;
  162. }
  163. static av_cold int eightsvx_decode_close(AVCodecContext *avctx)
  164. {
  165. EightSvxContext *esc = avctx->priv_data;
  166. av_freep(&esc->samples);
  167. esc->samples_size = 0;
  168. esc->samples_idx = 0;
  169. return 0;
  170. }
  171. AVCodec ff_eightsvx_fib_decoder = {
  172. .name = "8svx_fib",
  173. .type = AVMEDIA_TYPE_AUDIO,
  174. .id = CODEC_ID_8SVX_FIB,
  175. .priv_data_size = sizeof (EightSvxContext),
  176. .init = eightsvx_decode_init,
  177. .decode = eightsvx_decode_frame,
  178. .close = eightsvx_decode_close,
  179. .long_name = NULL_IF_CONFIG_SMALL("8SVX fibonacci"),
  180. };
  181. AVCodec ff_eightsvx_exp_decoder = {
  182. .name = "8svx_exp",
  183. .type = AVMEDIA_TYPE_AUDIO,
  184. .id = CODEC_ID_8SVX_EXP,
  185. .priv_data_size = sizeof (EightSvxContext),
  186. .init = eightsvx_decode_init,
  187. .decode = eightsvx_decode_frame,
  188. .close = eightsvx_decode_close,
  189. .long_name = NULL_IF_CONFIG_SMALL("8SVX exponential"),
  190. };
  191. AVCodec ff_eightsvx_raw_decoder = {
  192. .name = "8svx_raw",
  193. .type = AVMEDIA_TYPE_AUDIO,
  194. .id = CODEC_ID_8SVX_RAW,
  195. .priv_data_size = sizeof(EightSvxContext),
  196. .init = eightsvx_decode_init,
  197. .decode = eightsvx_decode_frame,
  198. .close = eightsvx_decode_close,
  199. .long_name = NULL_IF_CONFIG_SMALL("8SVX rawaudio"),
  200. };