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.

244 lines
7.9KB

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