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.

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