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.

237 lines
7.3KB

  1. /*
  2. * 8SVX audio decoder
  3. * Copyright (C) 2008 Jaikrishnan Menon
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. #include "avcodec.h"
  30. /** decoder context */
  31. typedef struct EightSvxContext {
  32. uint8_t fib_acc[2];
  33. const int8_t *table;
  34. /* buffer used to store the whole first packet.
  35. data is only sent as one large packet */
  36. uint8_t *data[2];
  37. int data_size;
  38. int data_idx;
  39. } EightSvxContext;
  40. static const int8_t fibonacci[16] = { -34, -21, -13, -8, -5, -3, -2, -1,
  41. 0, 1, 2, 3, 5, 8, 13, 21 };
  42. static const int8_t exponential[16] = { -128, -64, -32, -16, -8, -4, -2, -1,
  43. 0, 1, 2, 4, 8, 16, 32, 64 };
  44. #define MAX_FRAME_SIZE 32768
  45. /**
  46. * Delta decode the compressed values in src, and put the resulting
  47. * decoded samples in dst.
  48. *
  49. * @param[in,out] state starting value. it is saved for use in the next call.
  50. */
  51. static void delta_decode(uint8_t *dst, const uint8_t *src, int src_size,
  52. uint8_t *state, const int8_t *table, int channels)
  53. {
  54. uint8_t val = *state;
  55. while (src_size--) {
  56. uint8_t d = *src++;
  57. val = av_clip_uint8(val + table[d & 0xF]);
  58. *dst = val;
  59. dst += channels;
  60. val = av_clip_uint8(val + table[d >> 4]);
  61. *dst = val;
  62. dst += channels;
  63. }
  64. *state = val;
  65. }
  66. static void raw_decode(uint8_t *dst, const int8_t *src, int src_size,
  67. int channels)
  68. {
  69. while (src_size--) {
  70. *dst = *src++ + 128;
  71. dst += channels;
  72. }
  73. }
  74. /** decode a frame */
  75. static int eightsvx_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  76. AVPacket *avpkt)
  77. {
  78. EightSvxContext *esc = avctx->priv_data;
  79. int buf_size;
  80. uint8_t *out_data = data;
  81. int out_data_size;
  82. int is_compr = (avctx->codec_id != CODEC_ID_PCM_S8_PLANAR);
  83. /* for the first packet, copy data to buffer */
  84. if (avpkt->data) {
  85. int hdr_size = is_compr ? 2 : 0;
  86. int chan_size = (avpkt->size - hdr_size * avctx->channels) / avctx->channels;
  87. if (avpkt->size < hdr_size * avctx->channels) {
  88. av_log(avctx, AV_LOG_ERROR, "packet size is too small\n");
  89. return AVERROR(EINVAL);
  90. }
  91. if (esc->data[0]) {
  92. av_log(avctx, AV_LOG_ERROR, "unexpected data after first packet\n");
  93. return AVERROR(EINVAL);
  94. }
  95. if (is_compr) {
  96. esc->fib_acc[0] = avpkt->data[1] + 128;
  97. if (avctx->channels == 2)
  98. esc->fib_acc[1] = avpkt->data[2+chan_size+1] + 128;
  99. }
  100. esc->data_idx = 0;
  101. esc->data_size = chan_size;
  102. if (!(esc->data[0] = av_malloc(chan_size)))
  103. return AVERROR(ENOMEM);
  104. if (avctx->channels == 2) {
  105. if (!(esc->data[1] = av_malloc(chan_size))) {
  106. av_freep(&esc->data[0]);
  107. return AVERROR(ENOMEM);
  108. }
  109. }
  110. memcpy(esc->data[0], &avpkt->data[hdr_size], chan_size);
  111. if (avctx->channels == 2)
  112. memcpy(esc->data[1], &avpkt->data[2*hdr_size+chan_size], chan_size);
  113. }
  114. if (!esc->data[0]) {
  115. av_log(avctx, AV_LOG_ERROR, "unexpected empty packet\n");
  116. return AVERROR(EINVAL);
  117. }
  118. /* decode next piece of data from the buffer */
  119. buf_size = FFMIN(MAX_FRAME_SIZE, esc->data_size - esc->data_idx);
  120. if (buf_size <= 0) {
  121. *data_size = 0;
  122. return avpkt->size;
  123. }
  124. out_data_size = buf_size * (is_compr + 1) * avctx->channels;
  125. if (*data_size < out_data_size) {
  126. av_log(avctx, AV_LOG_ERROR, "Provided buffer with size %d is too small.\n",
  127. *data_size);
  128. return AVERROR(EINVAL);
  129. }
  130. if (is_compr) {
  131. delta_decode(out_data, &esc->data[0][esc->data_idx], buf_size,
  132. &esc->fib_acc[0], esc->table, avctx->channels);
  133. if (avctx->channels == 2) {
  134. delta_decode(&out_data[1], &esc->data[1][esc->data_idx], buf_size,
  135. &esc->fib_acc[1], esc->table, avctx->channels);
  136. }
  137. } else {
  138. int ch;
  139. for (ch = 0; ch < avctx->channels; ch++) {
  140. raw_decode((int8_t *)&out_data[ch], &esc->data[ch][esc->data_idx],
  141. buf_size, avctx->channels);
  142. }
  143. }
  144. esc->data_idx += buf_size;
  145. *data_size = out_data_size;
  146. return avpkt->size;
  147. }
  148. /** initialize 8svx decoder */
  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(EINVAL);
  155. }
  156. switch(avctx->codec->id) {
  157. case CODEC_ID_8SVX_FIB:
  158. esc->table = fibonacci;
  159. break;
  160. case CODEC_ID_8SVX_EXP:
  161. esc->table = exponential;
  162. break;
  163. case CODEC_ID_PCM_S8_PLANAR:
  164. break;
  165. default:
  166. return -1;
  167. }
  168. avctx->sample_fmt = AV_SAMPLE_FMT_U8;
  169. return 0;
  170. }
  171. static av_cold int eightsvx_decode_close(AVCodecContext *avctx)
  172. {
  173. EightSvxContext *esc = avctx->priv_data;
  174. av_freep(&esc->data[0]);
  175. av_freep(&esc->data[1]);
  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. .close = eightsvx_decode_close,
  185. .decode = eightsvx_decode_frame,
  186. .capabilities = CODEC_CAP_DELAY,
  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. .close = eightsvx_decode_close,
  196. .decode = eightsvx_decode_frame,
  197. .capabilities = CODEC_CAP_DELAY,
  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_DELAY,
  209. .long_name = NULL_IF_CONFIG_SMALL("PCM signed 8-bit planar"),
  210. };