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.

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