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.

261 lines
8.3KB

  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 "libavutil/avassert.h"
  39. #include "avcodec.h"
  40. #include "libavutil/common.h"
  41. /** decoder context */
  42. typedef struct EightSvxContext {
  43. AVFrame frame;
  44. const int8_t *table;
  45. /* buffer used to store the whole audio decoded/interleaved chunk,
  46. * which is sent with the first packet */
  47. uint8_t *samples;
  48. int64_t samples_size;
  49. int samples_idx;
  50. } EightSvxContext;
  51. static const int8_t fibonacci[16] = { -34, -21, -13, -8, -5, -3, -2, -1, 0, 1, 2, 3, 5, 8, 13, 21 };
  52. static const int8_t exponential[16] = { -128, -64, -32, -16, -8, -4, -2, -1, 0, 1, 2, 4, 8, 16, 32, 64 };
  53. #define MAX_FRAME_SIZE 2048
  54. /**
  55. * Interleave samples in buffer containing all left channel samples
  56. * at the beginning, and right channel samples at the end.
  57. * Each sample is assumed to be in signed 8-bit format.
  58. *
  59. * @param size the size in bytes of the dst and src buffer
  60. */
  61. static void interleave_stereo(uint8_t *dst, const uint8_t *src, int size)
  62. {
  63. uint8_t *dst_end = dst + size;
  64. size = size>>1;
  65. while (dst < dst_end) {
  66. *dst++ = *src;
  67. *dst++ = *(src+size);
  68. src++;
  69. }
  70. }
  71. /**
  72. * Delta decode the compressed values in src, and put the resulting
  73. * decoded n samples in dst.
  74. *
  75. * @param val starting value assumed by the delta sequence
  76. * @param table delta sequence table
  77. * @return size in bytes of the decoded data, must be src_size*2
  78. */
  79. static int delta_decode(int8_t *dst, const uint8_t *src, int src_size,
  80. int8_t val, const int8_t *table)
  81. {
  82. int n = src_size;
  83. int8_t *dst0 = dst;
  84. while (n--) {
  85. uint8_t d = *src++;
  86. val = av_clip(val + table[d & 0x0f], -127, 128);
  87. *dst++ = val;
  88. val = av_clip(val + table[d >> 4] , -127, 128);
  89. *dst++ = val;
  90. }
  91. return dst-dst0;
  92. }
  93. /** decode a frame */
  94. static int eightsvx_decode_frame(AVCodecContext *avctx, void *data,
  95. int *got_frame_ptr, AVPacket *avpkt)
  96. {
  97. EightSvxContext *esc = avctx->priv_data;
  98. int n, out_data_size, ret;
  99. uint8_t *src, *dst;
  100. /* decode and interleave the first packet */
  101. if (!esc->samples && avpkt) {
  102. uint8_t *deinterleaved_samples, *p = NULL;
  103. int packet_size = avpkt->size;
  104. if (packet_size % avctx->channels) {
  105. av_log(avctx, AV_LOG_WARNING, "Packet with odd size, ignoring last byte\n");
  106. if (packet_size < avctx->channels)
  107. return packet_size;
  108. packet_size -= packet_size % avctx->channels;
  109. }
  110. esc->samples_size = !esc->table ?
  111. packet_size : avctx->channels + (packet_size-avctx->channels) * 2;
  112. if (!(esc->samples = av_malloc(esc->samples_size)))
  113. return AVERROR(ENOMEM);
  114. /* decompress */
  115. if (esc->table) {
  116. const uint8_t *buf = avpkt->data;
  117. uint8_t *dst;
  118. int buf_size = avpkt->size;
  119. int i, n = esc->samples_size;
  120. if (buf_size < 2) {
  121. av_log(avctx, AV_LOG_ERROR, "packet size is too small\n");
  122. return AVERROR(EINVAL);
  123. }
  124. if (!(deinterleaved_samples = av_mallocz(n)))
  125. return AVERROR(ENOMEM);
  126. dst = p = deinterleaved_samples;
  127. /* the uncompressed starting value is contained in the first byte */
  128. dst = deinterleaved_samples;
  129. for (i = 0; i < avctx->channels; i++) {
  130. delta_decode(dst, buf + 1, buf_size / avctx->channels - 1, buf[0], esc->table);
  131. buf += buf_size / avctx->channels;
  132. dst += n / avctx->channels - 1;
  133. }
  134. } else {
  135. deinterleaved_samples = avpkt->data;
  136. }
  137. if (avctx->channels == 2)
  138. interleave_stereo(esc->samples, deinterleaved_samples, esc->samples_size);
  139. else
  140. memcpy(esc->samples, deinterleaved_samples, esc->samples_size);
  141. av_freep(&p);
  142. }
  143. /* get output buffer */
  144. av_assert1(!(esc->samples_size % avctx->channels || esc->samples_idx % avctx->channels));
  145. esc->frame.nb_samples = FFMIN(MAX_FRAME_SIZE, esc->samples_size - esc->samples_idx) / avctx->channels;
  146. if ((ret = avctx->get_buffer(avctx, &esc->frame)) < 0) {
  147. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  148. return ret;
  149. }
  150. *got_frame_ptr = 1;
  151. *(AVFrame *)data = esc->frame;
  152. dst = esc->frame.data[0];
  153. src = esc->samples + esc->samples_idx;
  154. out_data_size = esc->frame.nb_samples * avctx->channels;
  155. for (n = out_data_size; n > 0; n--)
  156. *dst++ = *src++ + 128;
  157. esc->samples_idx += out_data_size;
  158. return esc->table ?
  159. (avctx->frame_number == 0)*2 + out_data_size / 2 :
  160. out_data_size;
  161. }
  162. static av_cold int eightsvx_decode_init(AVCodecContext *avctx)
  163. {
  164. EightSvxContext *esc = avctx->priv_data;
  165. if (avctx->channels < 1 || avctx->channels > 2) {
  166. av_log(avctx, AV_LOG_ERROR, "8SVX does not support more than 2 channels\n");
  167. return AVERROR_INVALIDDATA;
  168. }
  169. switch (avctx->codec->id) {
  170. case AV_CODEC_ID_8SVX_FIB: esc->table = fibonacci; break;
  171. case AV_CODEC_ID_8SVX_EXP: esc->table = exponential; break;
  172. case AV_CODEC_ID_PCM_S8_PLANAR:
  173. case AV_CODEC_ID_8SVX_RAW: esc->table = NULL; break;
  174. default:
  175. av_log(avctx, AV_LOG_ERROR, "Invalid codec id %d.\n", avctx->codec->id);
  176. return AVERROR_INVALIDDATA;
  177. }
  178. avctx->sample_fmt = AV_SAMPLE_FMT_U8;
  179. avcodec_get_frame_defaults(&esc->frame);
  180. avctx->coded_frame = &esc->frame;
  181. return 0;
  182. }
  183. static av_cold int eightsvx_decode_close(AVCodecContext *avctx)
  184. {
  185. EightSvxContext *esc = avctx->priv_data;
  186. av_freep(&esc->samples);
  187. esc->samples_size = 0;
  188. esc->samples_idx = 0;
  189. return 0;
  190. }
  191. #if CONFIG_EIGHTSVX_FIB_DECODER
  192. AVCodec ff_eightsvx_fib_decoder = {
  193. .name = "8svx_fib",
  194. .type = AVMEDIA_TYPE_AUDIO,
  195. .id = AV_CODEC_ID_8SVX_FIB,
  196. .priv_data_size = sizeof (EightSvxContext),
  197. .init = eightsvx_decode_init,
  198. .decode = eightsvx_decode_frame,
  199. .close = eightsvx_decode_close,
  200. .capabilities = CODEC_CAP_DR1,
  201. .long_name = NULL_IF_CONFIG_SMALL("8SVX fibonacci"),
  202. };
  203. #endif
  204. #if CONFIG_EIGHTSVX_EXP_DECODER
  205. AVCodec ff_eightsvx_exp_decoder = {
  206. .name = "8svx_exp",
  207. .type = AVMEDIA_TYPE_AUDIO,
  208. .id = AV_CODEC_ID_8SVX_EXP,
  209. .priv_data_size = sizeof (EightSvxContext),
  210. .init = eightsvx_decode_init,
  211. .decode = eightsvx_decode_frame,
  212. .close = eightsvx_decode_close,
  213. .capabilities = CODEC_CAP_DR1,
  214. .long_name = NULL_IF_CONFIG_SMALL("8SVX exponential"),
  215. };
  216. #endif
  217. #if CONFIG_PCM_S8_PLANAR_DECODER
  218. AVCodec ff_pcm_s8_planar_decoder = {
  219. .name = "pcm_s8_planar",
  220. .type = AVMEDIA_TYPE_AUDIO,
  221. .id = AV_CODEC_ID_PCM_S8_PLANAR,
  222. .priv_data_size = sizeof(EightSvxContext),
  223. .init = eightsvx_decode_init,
  224. .close = eightsvx_decode_close,
  225. .decode = eightsvx_decode_frame,
  226. .capabilities = CODEC_CAP_DR1,
  227. .long_name = NULL_IF_CONFIG_SMALL("PCM signed 8-bit planar"),
  228. };
  229. #endif