AVPacket *avpkt)
{
EightSvxContext *esc = avctx->priv_data;
- int buf_size;
- uint8_t *out_data = data;
- int out_data_size;
- int is_compr = (avctx->codec_id != CODEC_ID_PCM_S8_PLANAR);
-
- /* for the first packet, copy data to buffer */
- if (avpkt->data) {
- int hdr_size = is_compr ? 2 : 0;
- int chan_size = (avpkt->size - hdr_size * avctx->channels) / avctx->channels;
-
- if (avpkt->size < hdr_size * avctx->channels) {
- av_log(avctx, AV_LOG_ERROR, "packet size is too small\n");
- return AVERROR(EINVAL);
- }
- if (esc->data[0]) {
- av_log(avctx, AV_LOG_ERROR, "unexpected data after first packet\n");
- return AVERROR(EINVAL);
- }
+ int out_data_size, n;
+ uint8_t *src, *dst;
- if (is_compr) {
- esc->fib_acc[0] = avpkt->data[1] + 128;
- if (avctx->channels == 2)
- esc->fib_acc[1] = avpkt->data[2+chan_size+1] + 128;
- }
+ /* decode and interleave the first packet */
+ if (!esc->samples && avpkt) {
+ uint8_t *deinterleaved_samples;
- esc->samples_size = avctx->codec->id == CODEC_ID_8SVX_RAW ?
- esc->data_idx = 0;
- esc->data_size = chan_size;
- if (!(esc->data[0] = av_malloc(chan_size)))
++ esc->samples_size = avctx->codec->id == CODEC_ID_8SVX_RAW || avctx->codec->id ==CODEC_ID_PCM_S8_PLANAR?
+ avpkt->size : avctx->channels + (avpkt->size-avctx->channels) * 2;
+ if (!(esc->samples = av_malloc(esc->samples_size)))
return AVERROR(ENOMEM);
- if (avctx->channels == 2) {
- if (!(esc->data[1] = av_malloc(chan_size))) {
- av_freep(&esc->data[0]);
- return AVERROR(ENOMEM);
+
+ /* decompress */
+ if (avctx->codec->id == CODEC_ID_8SVX_FIB || avctx->codec->id == CODEC_ID_8SVX_EXP) {
+ const uint8_t *buf = avpkt->data;
+ int buf_size = avpkt->size;
+ int n = esc->samples_size;
+
+ if (buf_size < 2) {
+ av_log(avctx, AV_LOG_ERROR, "packet size is too small\n");
+ return AVERROR(EINVAL);
}
+ if (!(deinterleaved_samples = av_mallocz(n)))
+ return AVERROR(ENOMEM);
+
+ /* the uncompressed starting value is contained in the first byte */
+ if (avctx->channels == 2) {
+ delta_decode(deinterleaved_samples , buf+1, buf_size/2-1, buf[0], esc->table);
+ buf += buf_size/2;
+ delta_decode(deinterleaved_samples+n/2-1, buf+1, buf_size/2-1, buf[0], esc->table);
+ } else
+ delta_decode(deinterleaved_samples , buf+1, buf_size-1 , buf[0], esc->table);
+ } else {
+ deinterleaved_samples = avpkt->data;
}
- memcpy(esc->data[0], &avpkt->data[hdr_size], chan_size);
+
if (avctx->channels == 2)
- memcpy(esc->data[1], &avpkt->data[2*hdr_size+chan_size], chan_size);
- }
- if (!esc->data[0]) {
- av_log(avctx, AV_LOG_ERROR, "unexpected empty packet\n");
- return AVERROR(EINVAL);
+ interleave_stereo(esc->samples, deinterleaved_samples, esc->samples_size);
+ else
+ memcpy(esc->samples, deinterleaved_samples, esc->samples_size);
}
- /* decode next piece of data from the buffer */
- buf_size = FFMIN(MAX_FRAME_SIZE, esc->data_size - esc->data_idx);
- if (buf_size <= 0) {
- *data_size = 0;
- return avpkt->size;
- }
- out_data_size = buf_size * (is_compr + 1) * avctx->channels;
+ /* return single packed with fixed size */
+ out_data_size = FFMIN(MAX_FRAME_SIZE, esc->samples_size - esc->samples_idx);
if (*data_size < out_data_size) {
- av_log(avctx, AV_LOG_ERROR, "Provided buffer with size %d is too small.\n",
- *data_size);
+ av_log(avctx, AV_LOG_ERROR, "Provided buffer with size %d is too small.\n", *data_size);
return AVERROR(EINVAL);
}
- if (is_compr) {
- delta_decode(out_data, &esc->data[0][esc->data_idx], buf_size,
- &esc->fib_acc[0], esc->table, avctx->channels);
- if (avctx->channels == 2) {
- delta_decode(&out_data[1], &esc->data[1][esc->data_idx], buf_size,
- &esc->fib_acc[1], esc->table, avctx->channels);
- }
- } else {
- int ch;
- for (ch = 0; ch < avctx->channels; ch++) {
- raw_decode((int8_t *)&out_data[ch], &esc->data[ch][esc->data_idx],
- buf_size, avctx->channels);
- }
- }
- esc->data_idx += buf_size;
- *data_size = out_data_size;
- return avpkt->size;
+ *data_size = out_data_size;
+ dst = data;
+ src = esc->samples + esc->samples_idx;
+ for (n = out_data_size; n > 0; n--)
+ *dst++ = *src++ + 128;
+ esc->samples_idx += *data_size;
+
+ return avctx->codec->id == CODEC_ID_8SVX_FIB || avctx->codec->id == CODEC_ID_8SVX_EXP ?
+ (avctx->frame_number == 0)*2 + out_data_size / 2 :
+ out_data_size;
}
-/** initialize 8svx decoder */
static av_cold int eightsvx_decode_init(AVCodecContext *avctx)
{
EightSvxContext *esc = avctx->priv_data;
- if (avctx->channels > 2) {
+ if (avctx->channels < 1 || avctx->channels > 2) {
av_log(avctx, AV_LOG_ERROR, "8SVX does not support more than 2 channels\n");
- return AVERROR(EINVAL);
+ return AVERROR_INVALIDDATA;
}
- switch(avctx->codec->id) {
- case CODEC_ID_8SVX_FIB:
- esc->table = fibonacci;
- break;
- case CODEC_ID_8SVX_EXP:
- esc->table = exponential;
- break;
- case CODEC_ID_PCM_S8_PLANAR:
- break;
- default:
- return -1;
+ switch (avctx->codec->id) {
+ case CODEC_ID_8SVX_FIB: esc->table = fibonacci; break;
+ case CODEC_ID_8SVX_EXP: esc->table = exponential; break;
++ case CODEC_ID_PCM_S8_PLANAR:
+ case CODEC_ID_8SVX_RAW: esc->table = NULL; break;
+ default:
+ av_log(avctx, AV_LOG_ERROR, "Invalid codec id %d.\n", avctx->codec->id);
+ return AVERROR_INVALIDDATA;
}
avctx->sample_fmt = AV_SAMPLE_FMT_U8;
+
return 0;
}
.long_name = NULL_IF_CONFIG_SMALL("8SVX exponential"),
};
- AVCodec ff_eightsvx_raw_decoder = {
- .name = "8svx_raw",
- .type = AVMEDIA_TYPE_AUDIO,
- .id = CODEC_ID_8SVX_RAW,
- .priv_data_size = sizeof(EightSvxContext),
- .init = eightsvx_decode_init,
- .decode = eightsvx_decode_frame,
- .close = eightsvx_decode_close,
- .long_name = NULL_IF_CONFIG_SMALL("8SVX rawaudio"),
+ AVCodec ff_pcm_s8_planar_decoder = {
+ .name = "pcm_s8_planar",
+ .type = AVMEDIA_TYPE_AUDIO,
+ .id = CODEC_ID_PCM_S8_PLANAR,
+ .priv_data_size = sizeof(EightSvxContext),
+ .init = eightsvx_decode_init,
+ .close = eightsvx_decode_close,
+ .decode = eightsvx_decode_frame,
- .capabilities = CODEC_CAP_DELAY,
+ .long_name = NULL_IF_CONFIG_SMALL("PCM signed 8-bit planar"),
};