00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00040 #include "avcodec.h"
00041
00043 typedef struct EightSvxContext {
00044 AVFrame frame;
00045 const int8_t *table;
00046
00047
00048
00049 uint8_t *samples;
00050 size_t samples_size;
00051 int samples_idx;
00052 } EightSvxContext;
00053
00054 static const int8_t fibonacci[16] = { -34, -21, -13, -8, -5, -3, -2, -1, 0, 1, 2, 3, 5, 8, 13, 21 };
00055 static const int8_t exponential[16] = { -128, -64, -32, -16, -8, -4, -2, -1, 0, 1, 2, 4, 8, 16, 32, 64 };
00056
00057 #define MAX_FRAME_SIZE 2048
00058
00066 static void interleave_stereo(uint8_t *dst, const uint8_t *src, int size)
00067 {
00068 uint8_t *dst_end = dst + size;
00069 size = size>>1;
00070
00071 while (dst < dst_end) {
00072 *dst++ = *src;
00073 *dst++ = *(src+size);
00074 src++;
00075 }
00076 }
00077
00086 static int delta_decode(int8_t *dst, const uint8_t *src, int src_size,
00087 int8_t val, const int8_t *table)
00088 {
00089 int n = src_size;
00090 int8_t *dst0 = dst;
00091
00092 while (n--) {
00093 uint8_t d = *src++;
00094 val = av_clip(val + table[d & 0x0f], -127, 128);
00095 *dst++ = val;
00096 val = av_clip(val + table[d >> 4] , -127, 128);
00097 *dst++ = val;
00098 }
00099
00100 return dst-dst0;
00101 }
00102
00104 static int eightsvx_decode_frame(AVCodecContext *avctx, void *data,
00105 int *got_frame_ptr, AVPacket *avpkt)
00106 {
00107 EightSvxContext *esc = avctx->priv_data;
00108 int n, out_data_size, ret;
00109 uint8_t *out_date;
00110 uint8_t *src, *dst;
00111
00112
00113 if (!esc->samples && avpkt) {
00114 uint8_t *deinterleaved_samples;
00115
00116 esc->samples_size = avctx->codec->id == CODEC_ID_8SVX_RAW || avctx->codec->id ==CODEC_ID_PCM_S8_PLANAR?
00117 avpkt->size : avctx->channels + (avpkt->size-avctx->channels) * 2;
00118 if (!(esc->samples = av_malloc(esc->samples_size)))
00119 return AVERROR(ENOMEM);
00120
00121
00122 if (avctx->codec->id == CODEC_ID_8SVX_FIB || avctx->codec->id == CODEC_ID_8SVX_EXP) {
00123 const uint8_t *buf = avpkt->data;
00124 int buf_size = avpkt->size;
00125 int n = esc->samples_size;
00126
00127 if (buf_size < 2) {
00128 av_log(avctx, AV_LOG_ERROR, "packet size is too small\n");
00129 return AVERROR(EINVAL);
00130 }
00131 if (!(deinterleaved_samples = av_mallocz(n)))
00132 return AVERROR(ENOMEM);
00133
00134
00135 if (avctx->channels == 2) {
00136 delta_decode(deinterleaved_samples , buf+1, buf_size/2-1, buf[0], esc->table);
00137 buf += buf_size/2;
00138 delta_decode(deinterleaved_samples+n/2-1, buf+1, buf_size/2-1, buf[0], esc->table);
00139 } else
00140 delta_decode(deinterleaved_samples , buf+1, buf_size-1 , buf[0], esc->table);
00141 } else {
00142 deinterleaved_samples = avpkt->data;
00143 }
00144
00145 if (avctx->channels == 2)
00146 interleave_stereo(esc->samples, deinterleaved_samples, esc->samples_size);
00147 else
00148 memcpy(esc->samples, deinterleaved_samples, esc->samples_size);
00149 }
00150
00151
00152 esc->frame.nb_samples = (FFMIN(MAX_FRAME_SIZE, esc->samples_size - esc->samples_idx) +avctx->channels-1) / avctx->channels;
00153 if ((ret = avctx->get_buffer(avctx, &esc->frame)) < 0) {
00154 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00155 return ret;
00156 }
00157
00158 *got_frame_ptr = 1;
00159 *(AVFrame *)data = esc->frame;
00160
00161 dst = esc->frame.data[0];
00162 src = esc->samples + esc->samples_idx;
00163 out_data_size = esc->frame.nb_samples * avctx->channels;
00164 for (n = out_data_size; n > 0; n--)
00165 *dst++ = *src++ + 128;
00166 esc->samples_idx += out_data_size;
00167
00168 return avctx->codec->id == CODEC_ID_8SVX_FIB || avctx->codec->id == CODEC_ID_8SVX_EXP ?
00169 (avctx->frame_number == 0)*2 + out_data_size / 2 :
00170 out_data_size;
00171 }
00172
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_INVALIDDATA;
00180 }
00181
00182 switch (avctx->codec->id) {
00183 case CODEC_ID_8SVX_FIB: esc->table = fibonacci; break;
00184 case CODEC_ID_8SVX_EXP: esc->table = exponential; break;
00185 case CODEC_ID_PCM_S8_PLANAR:
00186 case CODEC_ID_8SVX_RAW: esc->table = NULL; break;
00187 default:
00188 av_log(avctx, AV_LOG_ERROR, "Invalid codec id %d.\n", avctx->codec->id);
00189 return AVERROR_INVALIDDATA;
00190 }
00191 avctx->sample_fmt = AV_SAMPLE_FMT_U8;
00192
00193 avcodec_get_frame_defaults(&esc->frame);
00194 avctx->coded_frame = &esc->frame;
00195
00196 return 0;
00197 }
00198
00199 static av_cold int eightsvx_decode_close(AVCodecContext *avctx)
00200 {
00201 EightSvxContext *esc = avctx->priv_data;
00202
00203 av_freep(&esc->samples);
00204 esc->samples_size = 0;
00205 esc->samples_idx = 0;
00206
00207 return 0;
00208 }
00209
00210 AVCodec ff_eightsvx_fib_decoder = {
00211 .name = "8svx_fib",
00212 .type = AVMEDIA_TYPE_AUDIO,
00213 .id = CODEC_ID_8SVX_FIB,
00214 .priv_data_size = sizeof (EightSvxContext),
00215 .init = eightsvx_decode_init,
00216 .decode = eightsvx_decode_frame,
00217 .close = eightsvx_decode_close,
00218 .capabilities = CODEC_CAP_DR1,
00219 .long_name = NULL_IF_CONFIG_SMALL("8SVX fibonacci"),
00220 };
00221
00222 AVCodec ff_eightsvx_exp_decoder = {
00223 .name = "8svx_exp",
00224 .type = AVMEDIA_TYPE_AUDIO,
00225 .id = CODEC_ID_8SVX_EXP,
00226 .priv_data_size = sizeof (EightSvxContext),
00227 .init = eightsvx_decode_init,
00228 .decode = eightsvx_decode_frame,
00229 .close = eightsvx_decode_close,
00230 .capabilities = CODEC_CAP_DR1,
00231 .long_name = NULL_IF_CONFIG_SMALL("8SVX exponential"),
00232 };
00233
00234 AVCodec ff_pcm_s8_planar_decoder = {
00235 .name = "pcm_s8_planar",
00236 .type = AVMEDIA_TYPE_AUDIO,
00237 .id = CODEC_ID_PCM_S8_PLANAR,
00238 .priv_data_size = sizeof(EightSvxContext),
00239 .init = eightsvx_decode_init,
00240 .close = eightsvx_decode_close,
00241 .decode = eightsvx_decode_frame,
00242 .capabilities = CODEC_CAP_DR1,
00243 .long_name = NULL_IF_CONFIG_SMALL("PCM signed 8-bit planar"),
00244 };