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.

253 lines
8.1KB

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