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.

223 lines
6.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 "libavutil/avassert.h"
  39. #include "avcodec.h"
  40. #include "internal.h"
  41. #include "libavutil/common.h"
  42. /** decoder context */
  43. typedef struct EightSvxContext {
  44. AVFrame frame;
  45. uint8_t fib_acc[2];
  46. const int8_t *table;
  47. /* buffer used to store the whole first packet.
  48. data is only sent as one large packet */
  49. uint8_t *data[2];
  50. int data_size;
  51. int data_idx;
  52. } EightSvxContext;
  53. static const int8_t fibonacci[16] = { -34, -21, -13, -8, -5, -3, -2, -1, 0, 1, 2, 3, 5, 8, 13, 21 };
  54. static const int8_t exponential[16] = { -128, -64, -32, -16, -8, -4, -2, -1, 0, 1, 2, 4, 8, 16, 32, 64 };
  55. #define MAX_FRAME_SIZE 2048
  56. /**
  57. * Delta decode the compressed values in src, and put the resulting
  58. * decoded samples in dst.
  59. *
  60. * @param[in,out] state starting value. it is saved for use in the next call.
  61. * @param table delta sequence table
  62. */
  63. static void delta_decode(uint8_t *dst, const uint8_t *src, int src_size,
  64. uint8_t *state, const int8_t *table)
  65. {
  66. uint8_t val = *state;
  67. while (src_size--) {
  68. uint8_t d = *src++;
  69. val = av_clip_uint8(val + table[d & 0xF]);
  70. *dst++ = val;
  71. val = av_clip_uint8(val + table[d >> 4]);
  72. *dst++ = val;
  73. }
  74. *state = val;
  75. }
  76. /** decode a frame */
  77. static int eightsvx_decode_frame(AVCodecContext *avctx, void *data,
  78. int *got_frame_ptr, AVPacket *avpkt)
  79. {
  80. EightSvxContext *esc = avctx->priv_data;
  81. int buf_size;
  82. int ch, ret;
  83. int hdr_size = 2;
  84. /* decode and interleave the first packet */
  85. if (!esc->data[0] && avpkt) {
  86. int chan_size = avpkt->size / avctx->channels - hdr_size;
  87. if (avpkt->size % avctx->channels) {
  88. av_log(avctx, AV_LOG_WARNING, "Packet with odd size, ignoring last byte\n");
  89. }
  90. if (avpkt->size < (hdr_size + 1) * avctx->channels) {
  91. av_log(avctx, AV_LOG_ERROR, "packet size is too small\n");
  92. return AVERROR(EINVAL);
  93. }
  94. esc->fib_acc[0] = avpkt->data[1] + 128;
  95. if (avctx->channels == 2)
  96. esc->fib_acc[1] = avpkt->data[2+chan_size+1] + 128;
  97. esc->data_idx = 0;
  98. esc->data_size = chan_size;
  99. if (!(esc->data[0] = av_malloc(chan_size)))
  100. return AVERROR(ENOMEM);
  101. if (avctx->channels == 2) {
  102. if (!(esc->data[1] = av_malloc(chan_size))) {
  103. av_freep(&esc->data[0]);
  104. return AVERROR(ENOMEM);
  105. }
  106. }
  107. memcpy(esc->data[0], &avpkt->data[hdr_size], chan_size);
  108. if (avctx->channels == 2)
  109. memcpy(esc->data[1], &avpkt->data[2*hdr_size+chan_size], chan_size);
  110. }
  111. if (!esc->data[0]) {
  112. av_log(avctx, AV_LOG_ERROR, "unexpected empty packet\n");
  113. return AVERROR(EINVAL);
  114. }
  115. /* decode next piece of data from the buffer */
  116. buf_size = FFMIN(MAX_FRAME_SIZE, esc->data_size - esc->data_idx);
  117. if (buf_size <= 0) {
  118. *got_frame_ptr = 0;
  119. return avpkt->size;
  120. }
  121. /* get output buffer */
  122. esc->frame.nb_samples = buf_size * 2;
  123. if ((ret = ff_get_buffer(avctx, &esc->frame)) < 0) {
  124. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  125. return ret;
  126. }
  127. for (ch = 0; ch < avctx->channels; ch++) {
  128. delta_decode(esc->frame.data[ch], &esc->data[ch][esc->data_idx],
  129. buf_size, &esc->fib_acc[ch], esc->table);
  130. }
  131. esc->data_idx += buf_size;
  132. *got_frame_ptr = 1;
  133. *(AVFrame *)data = esc->frame;
  134. return ((avctx->frame_number == 0)*hdr_size + buf_size)*avctx->channels;
  135. }
  136. static av_cold int eightsvx_decode_init(AVCodecContext *avctx)
  137. {
  138. EightSvxContext *esc = avctx->priv_data;
  139. if (avctx->channels < 1 || avctx->channels > 2) {
  140. av_log(avctx, AV_LOG_ERROR, "8SVX does not support more than 2 channels\n");
  141. return AVERROR_INVALIDDATA;
  142. }
  143. switch (avctx->codec->id) {
  144. case AV_CODEC_ID_8SVX_FIB: esc->table = fibonacci; break;
  145. case AV_CODEC_ID_8SVX_EXP: esc->table = exponential; break;
  146. default:
  147. av_log(avctx, AV_LOG_ERROR, "Invalid codec id %d.\n", avctx->codec->id);
  148. return AVERROR_INVALIDDATA;
  149. }
  150. avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
  151. avcodec_get_frame_defaults(&esc->frame);
  152. avctx->coded_frame = &esc->frame;
  153. return 0;
  154. }
  155. static av_cold int eightsvx_decode_close(AVCodecContext *avctx)
  156. {
  157. EightSvxContext *esc = avctx->priv_data;
  158. av_freep(&esc->data[0]);
  159. av_freep(&esc->data[1]);
  160. esc->data_size = 0;
  161. esc->data_idx = 0;
  162. return 0;
  163. }
  164. #if CONFIG_EIGHTSVX_FIB_DECODER
  165. AVCodec ff_eightsvx_fib_decoder = {
  166. .name = "8svx_fib",
  167. .type = AVMEDIA_TYPE_AUDIO,
  168. .id = AV_CODEC_ID_8SVX_FIB,
  169. .priv_data_size = sizeof (EightSvxContext),
  170. .init = eightsvx_decode_init,
  171. .decode = eightsvx_decode_frame,
  172. .close = eightsvx_decode_close,
  173. .capabilities = CODEC_CAP_DR1,
  174. .long_name = NULL_IF_CONFIG_SMALL("8SVX fibonacci"),
  175. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
  176. AV_SAMPLE_FMT_NONE },
  177. };
  178. #endif
  179. #if CONFIG_EIGHTSVX_EXP_DECODER
  180. AVCodec ff_eightsvx_exp_decoder = {
  181. .name = "8svx_exp",
  182. .type = AVMEDIA_TYPE_AUDIO,
  183. .id = AV_CODEC_ID_8SVX_EXP,
  184. .priv_data_size = sizeof (EightSvxContext),
  185. .init = eightsvx_decode_init,
  186. .decode = eightsvx_decode_frame,
  187. .close = eightsvx_decode_close,
  188. .capabilities = CODEC_CAP_DR1,
  189. .long_name = NULL_IF_CONFIG_SMALL("8SVX exponential"),
  190. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
  191. AV_SAMPLE_FMT_NONE },
  192. };
  193. #endif