8svx.c 6.7 KB

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