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.

248 lines
7.5KB

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