00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00031 #include "avcodec.h"
00032
00034 typedef struct EightSvxContext {
00035 AVFrame frame;
00036 uint8_t fib_acc[2];
00037 const int8_t *table;
00038
00039
00040
00041 uint8_t *data[2];
00042 int data_size;
00043 int data_idx;
00044 } EightSvxContext;
00045
00046 static const int8_t fibonacci[16] = { -34, -21, -13, -8, -5, -3, -2, -1,
00047 0, 1, 2, 3, 5, 8, 13, 21 };
00048 static const int8_t exponential[16] = { -128, -64, -32, -16, -8, -4, -2, -1,
00049 0, 1, 2, 4, 8, 16, 32, 64 };
00050
00051 #define MAX_FRAME_SIZE 32768
00052
00059 static void delta_decode(uint8_t *dst, const uint8_t *src, int src_size,
00060 uint8_t *state, const int8_t *table, int channels)
00061 {
00062 uint8_t val = *state;
00063
00064 while (src_size--) {
00065 uint8_t d = *src++;
00066 val = av_clip_uint8(val + table[d & 0xF]);
00067 *dst = val;
00068 dst += channels;
00069 val = av_clip_uint8(val + table[d >> 4]);
00070 *dst = val;
00071 dst += channels;
00072 }
00073
00074 *state = val;
00075 }
00076
00077 static void raw_decode(uint8_t *dst, const int8_t *src, int src_size,
00078 int channels)
00079 {
00080 while (src_size--) {
00081 *dst = *src++ + 128;
00082 dst += channels;
00083 }
00084 }
00085
00087 static int eightsvx_decode_frame(AVCodecContext *avctx, void *data,
00088 int *got_frame_ptr, AVPacket *avpkt)
00089 {
00090 EightSvxContext *esc = avctx->priv_data;
00091 int buf_size;
00092 uint8_t *out_data;
00093 int ret;
00094 int is_compr = (avctx->codec_id != CODEC_ID_PCM_S8_PLANAR);
00095
00096
00097 if (avpkt->data) {
00098 int hdr_size = is_compr ? 2 : 0;
00099 int chan_size = (avpkt->size - hdr_size * avctx->channels) / avctx->channels;
00100
00101 if (avpkt->size < hdr_size * avctx->channels) {
00102 av_log(avctx, AV_LOG_ERROR, "packet size is too small\n");
00103 return AVERROR(EINVAL);
00104 }
00105 if (esc->data[0]) {
00106 av_log(avctx, AV_LOG_ERROR, "unexpected data after first packet\n");
00107 return AVERROR(EINVAL);
00108 }
00109
00110 if (is_compr) {
00111 esc->fib_acc[0] = avpkt->data[1] + 128;
00112 if (avctx->channels == 2)
00113 esc->fib_acc[1] = avpkt->data[2+chan_size+1] + 128;
00114 }
00115
00116 esc->data_idx = 0;
00117 esc->data_size = chan_size;
00118 if (!(esc->data[0] = av_malloc(chan_size)))
00119 return AVERROR(ENOMEM);
00120 if (avctx->channels == 2) {
00121 if (!(esc->data[1] = av_malloc(chan_size))) {
00122 av_freep(&esc->data[0]);
00123 return AVERROR(ENOMEM);
00124 }
00125 }
00126 memcpy(esc->data[0], &avpkt->data[hdr_size], chan_size);
00127 if (avctx->channels == 2)
00128 memcpy(esc->data[1], &avpkt->data[2*hdr_size+chan_size], chan_size);
00129 }
00130 if (!esc->data[0]) {
00131 av_log(avctx, AV_LOG_ERROR, "unexpected empty packet\n");
00132 return AVERROR(EINVAL);
00133 }
00134
00135
00136 buf_size = FFMIN(MAX_FRAME_SIZE, esc->data_size - esc->data_idx);
00137 if (buf_size <= 0) {
00138 *got_frame_ptr = 0;
00139 return avpkt->size;
00140 }
00141
00142
00143 esc->frame.nb_samples = buf_size * (is_compr + 1);
00144 if ((ret = avctx->get_buffer(avctx, &esc->frame)) < 0) {
00145 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00146 return ret;
00147 }
00148 out_data = esc->frame.data[0];
00149
00150 if (is_compr) {
00151 delta_decode(out_data, &esc->data[0][esc->data_idx], buf_size,
00152 &esc->fib_acc[0], esc->table, avctx->channels);
00153 if (avctx->channels == 2) {
00154 delta_decode(&out_data[1], &esc->data[1][esc->data_idx], buf_size,
00155 &esc->fib_acc[1], esc->table, avctx->channels);
00156 }
00157 } else {
00158 int ch;
00159 for (ch = 0; ch < avctx->channels; ch++) {
00160 raw_decode((int8_t *)&out_data[ch], &esc->data[ch][esc->data_idx],
00161 buf_size, avctx->channels);
00162 }
00163 }
00164 esc->data_idx += buf_size;
00165
00166 *got_frame_ptr = 1;
00167 *(AVFrame *)data = esc->frame;
00168
00169 return avpkt->size;
00170 }
00171
00173 static av_cold int eightsvx_decode_init(AVCodecContext *avctx)
00174 {
00175 EightSvxContext *esc = avctx->priv_data;
00176
00177 if (avctx->channels < 1 || avctx->channels > 2) {
00178 av_log(avctx, AV_LOG_ERROR, "8SVX does not support more than 2 channels\n");
00179 return AVERROR(EINVAL);
00180 }
00181
00182 switch(avctx->codec->id) {
00183 case CODEC_ID_8SVX_FIB:
00184 esc->table = fibonacci;
00185 break;
00186 case CODEC_ID_8SVX_EXP:
00187 esc->table = exponential;
00188 break;
00189 case CODEC_ID_PCM_S8_PLANAR:
00190 break;
00191 default:
00192 return -1;
00193 }
00194 avctx->sample_fmt = AV_SAMPLE_FMT_U8;
00195
00196 avcodec_get_frame_defaults(&esc->frame);
00197 avctx->coded_frame = &esc->frame;
00198
00199 return 0;
00200 }
00201
00202 static av_cold int eightsvx_decode_close(AVCodecContext *avctx)
00203 {
00204 EightSvxContext *esc = avctx->priv_data;
00205
00206 av_freep(&esc->data[0]);
00207 av_freep(&esc->data[1]);
00208
00209 return 0;
00210 }
00211
00212 AVCodec ff_eightsvx_fib_decoder = {
00213 .name = "8svx_fib",
00214 .type = AVMEDIA_TYPE_AUDIO,
00215 .id = CODEC_ID_8SVX_FIB,
00216 .priv_data_size = sizeof (EightSvxContext),
00217 .init = eightsvx_decode_init,
00218 .close = eightsvx_decode_close,
00219 .decode = eightsvx_decode_frame,
00220 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
00221 .long_name = NULL_IF_CONFIG_SMALL("8SVX fibonacci"),
00222 };
00223
00224 AVCodec ff_eightsvx_exp_decoder = {
00225 .name = "8svx_exp",
00226 .type = AVMEDIA_TYPE_AUDIO,
00227 .id = CODEC_ID_8SVX_EXP,
00228 .priv_data_size = sizeof (EightSvxContext),
00229 .init = eightsvx_decode_init,
00230 .close = eightsvx_decode_close,
00231 .decode = eightsvx_decode_frame,
00232 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
00233 .long_name = NULL_IF_CONFIG_SMALL("8SVX exponential"),
00234 };
00235
00236 AVCodec ff_pcm_s8_planar_decoder = {
00237 .name = "pcm_s8_planar",
00238 .type = AVMEDIA_TYPE_AUDIO,
00239 .id = CODEC_ID_PCM_S8_PLANAR,
00240 .priv_data_size = sizeof(EightSvxContext),
00241 .init = eightsvx_decode_init,
00242 .close = eightsvx_decode_close,
00243 .decode = eightsvx_decode_frame,
00244 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
00245 .long_name = NULL_IF_CONFIG_SMALL("PCM signed 8-bit planar"),
00246 };