00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00040 #include "libavutil/avassert.h"
00041 #include "avcodec.h"
00042 #include "internal.h"
00043 #include "libavutil/common.h"
00044
00046 typedef struct EightSvxContext {
00047 AVFrame frame;
00048 uint8_t fib_acc[2];
00049 const int8_t *table;
00050
00051
00052
00053 uint8_t *data[2];
00054 int data_size;
00055 int data_idx;
00056 } EightSvxContext;
00057
00058 static const int8_t fibonacci[16] = { -34, -21, -13, -8, -5, -3, -2, -1, 0, 1, 2, 3, 5, 8, 13, 21 };
00059 static const int8_t exponential[16] = { -128, -64, -32, -16, -8, -4, -2, -1, 0, 1, 2, 4, 8, 16, 32, 64 };
00060
00061 #define MAX_FRAME_SIZE 2048
00062
00070 static void delta_decode(uint8_t *dst, const uint8_t *src, int src_size,
00071 uint8_t *state, const int8_t *table)
00072 {
00073 uint8_t val = *state;
00074
00075 while (src_size--) {
00076 uint8_t d = *src++;
00077 val = av_clip_uint8(val + table[d & 0xF]);
00078 *dst++ = val;
00079 val = av_clip_uint8(val + table[d >> 4]);
00080 *dst++ = val;
00081 }
00082
00083 *state = val;
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 int ch, ret;
00093 int hdr_size = 2;
00094
00095
00096 if (!esc->data[0] && avpkt) {
00097 int chan_size = avpkt->size / avctx->channels - hdr_size;
00098
00099 if (avpkt->size % avctx->channels) {
00100 av_log(avctx, AV_LOG_WARNING, "Packet with odd size, ignoring last byte\n");
00101 }
00102 if (avpkt->size < (hdr_size + 1) * avctx->channels) {
00103 av_log(avctx, AV_LOG_ERROR, "packet size is too small\n");
00104 return AVERROR(EINVAL);
00105 }
00106
00107 esc->fib_acc[0] = avpkt->data[1] + 128;
00108 if (avctx->channels == 2)
00109 esc->fib_acc[1] = avpkt->data[2+chan_size+1] + 128;
00110
00111 esc->data_idx = 0;
00112 esc->data_size = chan_size;
00113 if (!(esc->data[0] = av_malloc(chan_size)))
00114 return AVERROR(ENOMEM);
00115 if (avctx->channels == 2) {
00116 if (!(esc->data[1] = av_malloc(chan_size))) {
00117 av_freep(&esc->data[0]);
00118 return AVERROR(ENOMEM);
00119 }
00120 }
00121 memcpy(esc->data[0], &avpkt->data[hdr_size], chan_size);
00122 if (avctx->channels == 2)
00123 memcpy(esc->data[1], &avpkt->data[2*hdr_size+chan_size], chan_size);
00124 }
00125 if (!esc->data[0]) {
00126 av_log(avctx, AV_LOG_ERROR, "unexpected empty packet\n");
00127 return AVERROR(EINVAL);
00128 }
00129
00130
00131 buf_size = FFMIN(MAX_FRAME_SIZE, esc->data_size - esc->data_idx);
00132 if (buf_size <= 0) {
00133 *got_frame_ptr = 0;
00134 return avpkt->size;
00135 }
00136
00137
00138 esc->frame.nb_samples = buf_size * 2;
00139 if ((ret = ff_get_buffer(avctx, &esc->frame)) < 0) {
00140 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00141 return ret;
00142 }
00143
00144 for (ch = 0; ch < avctx->channels; ch++) {
00145 delta_decode(esc->frame.data[ch], &esc->data[ch][esc->data_idx],
00146 buf_size, &esc->fib_acc[ch], esc->table);
00147 }
00148
00149 esc->data_idx += buf_size;
00150
00151 *got_frame_ptr = 1;
00152 *(AVFrame *)data = esc->frame;
00153
00154 return ((avctx->frame_number == 0)*hdr_size + buf_size)*avctx->channels;
00155 }
00156
00157 static av_cold int eightsvx_decode_init(AVCodecContext *avctx)
00158 {
00159 EightSvxContext *esc = avctx->priv_data;
00160
00161 if (avctx->channels < 1 || avctx->channels > 2) {
00162 av_log(avctx, AV_LOG_ERROR, "8SVX does not support more than 2 channels\n");
00163 return AVERROR_INVALIDDATA;
00164 }
00165
00166 switch (avctx->codec->id) {
00167 case AV_CODEC_ID_8SVX_FIB: esc->table = fibonacci; break;
00168 case AV_CODEC_ID_8SVX_EXP: esc->table = exponential; break;
00169 default:
00170 av_log(avctx, AV_LOG_ERROR, "Invalid codec id %d.\n", avctx->codec->id);
00171 return AVERROR_INVALIDDATA;
00172 }
00173 avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
00174
00175 avcodec_get_frame_defaults(&esc->frame);
00176 avctx->coded_frame = &esc->frame;
00177
00178 return 0;
00179 }
00180
00181 static av_cold int eightsvx_decode_close(AVCodecContext *avctx)
00182 {
00183 EightSvxContext *esc = avctx->priv_data;
00184
00185 av_freep(&esc->data[0]);
00186 av_freep(&esc->data[1]);
00187 esc->data_size = 0;
00188 esc->data_idx = 0;
00189
00190 return 0;
00191 }
00192
00193 #if CONFIG_EIGHTSVX_FIB_DECODER
00194 AVCodec ff_eightsvx_fib_decoder = {
00195 .name = "8svx_fib",
00196 .type = AVMEDIA_TYPE_AUDIO,
00197 .id = AV_CODEC_ID_8SVX_FIB,
00198 .priv_data_size = sizeof (EightSvxContext),
00199 .init = eightsvx_decode_init,
00200 .decode = eightsvx_decode_frame,
00201 .close = eightsvx_decode_close,
00202 .capabilities = CODEC_CAP_DR1,
00203 .long_name = NULL_IF_CONFIG_SMALL("8SVX fibonacci"),
00204 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
00205 AV_SAMPLE_FMT_NONE },
00206 };
00207 #endif
00208 #if CONFIG_EIGHTSVX_EXP_DECODER
00209 AVCodec ff_eightsvx_exp_decoder = {
00210 .name = "8svx_exp",
00211 .type = AVMEDIA_TYPE_AUDIO,
00212 .id = AV_CODEC_ID_8SVX_EXP,
00213 .priv_data_size = sizeof (EightSvxContext),
00214 .init = eightsvx_decode_init,
00215 .decode = eightsvx_decode_frame,
00216 .close = eightsvx_decode_close,
00217 .capabilities = CODEC_CAP_DR1,
00218 .long_name = NULL_IF_CONFIG_SMALL("8SVX exponential"),
00219 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
00220 AV_SAMPLE_FMT_NONE },
00221 };
00222 #endif