8svx.c 7.46 KB
Newer Older
Jai Menon's avatar
Jai Menon committed
1
/*
Diego Biurrun's avatar
Diego Biurrun committed
2
 * 8SVX audio decoder
Jai Menon's avatar
Jai Menon committed
3 4
 * Copyright (C) 2008 Jaikrishnan Menon
 *
5
 * This file is part of Libav.
Jai Menon's avatar
Jai Menon committed
6
 *
7
 * Libav is free software; you can redistribute it and/or
Jai Menon's avatar
Jai Menon committed
8 9 10 11
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
12
 * Libav is distributed in the hope that it will be useful,
Jai Menon's avatar
Jai Menon committed
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with Libav; if not, write to the Free Software
Jai Menon's avatar
Jai Menon committed
19 20 21 22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
23
 * @file
Jai Menon's avatar
Jai Menon committed
24 25
 * 8svx audio decoder
 * @author Jaikrishnan Menon
26
 *
Jai Menon's avatar
Jai Menon committed
27 28 29 30 31
 * supports: fibonacci delta encoding
 *         : exponential encoding
 */

#include "avcodec.h"
32
#include "internal.h"
33
#include "libavutil/common.h"
Jai Menon's avatar
Jai Menon committed
34

35
/** decoder context */
Jai Menon's avatar
Jai Menon committed
36
typedef struct EightSvxContext {
37
    uint8_t fib_acc[2];
38
    const int8_t *table;
39 40 41 42 43 44

    /* buffer used to store the whole first packet.
       data is only sent as one large packet */
    uint8_t *data[2];
    int data_size;
    int data_idx;
Jai Menon's avatar
Jai Menon committed
45 46
} EightSvxContext;

47 48 49 50
static const int8_t fibonacci[16]   = { -34, -21, -13,  -8, -5, -3, -2, -1,
                                          0,   1,   2,   3,  5,  8, 13, 21 };
static const int8_t exponential[16] = { -128, -64, -32, -16, -8, -4, -2, -1,
                                           0,   1,   2,   4,  8, 16, 32, 64 };
Jai Menon's avatar
Jai Menon committed
51

52 53
#define MAX_FRAME_SIZE 32768

54 55 56 57 58 59
/**
 * Delta decode the compressed values in src, and put the resulting
 * decoded samples in dst.
 *
 * @param[in,out] state starting value. it is saved for use in the next call.
 */
60
static void delta_decode(uint8_t *dst, const uint8_t *src, int src_size,
61
                         uint8_t *state, const int8_t *table)
62
{
63
    uint8_t val = *state;
64 65 66

    while (src_size--) {
        uint8_t d = *src++;
67
        val = av_clip_uint8(val + table[d & 0xF]);
68
        *dst++ = val;
69
        val = av_clip_uint8(val + table[d >> 4]);
70
        *dst++ = val;
71 72 73 74 75
    }

    *state = val;
}

76
static void raw_decode(uint8_t *dst, const int8_t *src, int src_size)
77
{
78 79
    while (src_size--)
        *dst++ = *src++ + 128;
80 81
}

82
/** decode a frame */
83 84
static int eightsvx_decode_frame(AVCodecContext *avctx, void *data,
                                 int *got_frame_ptr, AVPacket *avpkt)
Jai Menon's avatar
Jai Menon committed
85 86
{
    EightSvxContext *esc = avctx->priv_data;
87
    AVFrame *frame       = data;
88
    int buf_size;
89
    int ch, ret;
90
    int is_compr = (avctx->codec_id != AV_CODEC_ID_PCM_S8_PLANAR);
91 92 93

    /* for the first packet, copy data to buffer */
    if (avpkt->data) {
94 95
        int hdr_size  = is_compr ? 2 : 0;
        int chan_size = (avpkt->size - hdr_size * avctx->channels) / avctx->channels;
Jai Menon's avatar
Jai Menon committed
96

97
        if (avpkt->size < hdr_size * avctx->channels) {
98 99 100
            av_log(avctx, AV_LOG_ERROR, "packet size is too small\n");
            return AVERROR(EINVAL);
        }
101 102 103 104 105
        if (esc->data[0]) {
            av_log(avctx, AV_LOG_ERROR, "unexpected data after first packet\n");
            return AVERROR(EINVAL);
        }

106
        if (is_compr) {
107 108 109
        esc->fib_acc[0] = avpkt->data[1] + 128;
        if (avctx->channels == 2)
            esc->fib_acc[1] = avpkt->data[2+chan_size+1] + 128;
110
        }
111 112 113 114 115 116 117 118 119 120 121

        esc->data_idx  = 0;
        esc->data_size = chan_size;
        if (!(esc->data[0] = av_malloc(chan_size)))
            return AVERROR(ENOMEM);
        if (avctx->channels == 2) {
            if (!(esc->data[1] = av_malloc(chan_size))) {
                av_freep(&esc->data[0]);
                return AVERROR(ENOMEM);
            }
        }
122
        memcpy(esc->data[0], &avpkt->data[hdr_size], chan_size);
123
        if (avctx->channels == 2)
124
            memcpy(esc->data[1], &avpkt->data[2*hdr_size+chan_size], chan_size);
125 126 127 128
    }
    if (!esc->data[0]) {
        av_log(avctx, AV_LOG_ERROR, "unexpected empty packet\n");
        return AVERROR(EINVAL);
Jai Menon's avatar
Jai Menon committed
129 130
    }

131 132 133
    /* decode next piece of data from the buffer */
    buf_size = FFMIN(MAX_FRAME_SIZE, esc->data_size - esc->data_idx);
    if (buf_size <= 0) {
134
        *got_frame_ptr = 0;
135 136
        return avpkt->size;
    }
137 138

    /* get output buffer */
139
    frame->nb_samples = buf_size * (is_compr + 1);
140
    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
141 142
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
        return ret;
143
    }
144 145 146

    for (ch = 0; ch < avctx->channels; ch++) {
        if (is_compr) {
147
            delta_decode(frame->data[ch], &esc->data[ch][esc->data_idx],
148 149
                         buf_size, &esc->fib_acc[ch], esc->table);
        } else {
150
            raw_decode(frame->data[ch], &esc->data[ch][esc->data_idx],
151
                       buf_size);
152 153
        }
    }
154

155
    esc->data_idx += buf_size;
156

157
    *got_frame_ptr = 1;
Jai Menon's avatar
Jai Menon committed
158

159
    return avpkt->size;
Jai Menon's avatar
Jai Menon committed
160 161
}

162
/** initialize 8svx decoder */
Jai Menon's avatar
Jai Menon committed
163 164 165 166
static av_cold int eightsvx_decode_init(AVCodecContext *avctx)
{
    EightSvxContext *esc = avctx->priv_data;

167 168 169 170 171
    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);
    }

Jai Menon's avatar
Jai Menon committed
172
    switch(avctx->codec->id) {
173
        case AV_CODEC_ID_8SVX_FIB:
Jai Menon's avatar
Jai Menon committed
174 175
          esc->table = fibonacci;
          break;
176
        case AV_CODEC_ID_8SVX_EXP:
Jai Menon's avatar
Jai Menon committed
177 178
          esc->table = exponential;
          break;
179
        case AV_CODEC_ID_PCM_S8_PLANAR:
180
            break;
Jai Menon's avatar
Jai Menon committed
181 182 183
        default:
          return -1;
    }
184
    avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
185

Jai Menon's avatar
Jai Menon committed
186 187 188
    return 0;
}

189 190 191 192 193 194 195 196 197 198
static av_cold int eightsvx_decode_close(AVCodecContext *avctx)
{
    EightSvxContext *esc = avctx->priv_data;

    av_freep(&esc->data[0]);
    av_freep(&esc->data[1]);

    return 0;
}

199
AVCodec ff_eightsvx_fib_decoder = {
200
  .name           = "8svx_fib",
201
  .long_name      = NULL_IF_CONFIG_SMALL("8SVX fibonacci"),
202
  .type           = AVMEDIA_TYPE_AUDIO,
203
  .id             = AV_CODEC_ID_8SVX_FIB,
Jai Menon's avatar
Jai Menon committed
204 205
  .priv_data_size = sizeof (EightSvxContext),
  .init           = eightsvx_decode_init,
206
  .close          = eightsvx_decode_close,
Jai Menon's avatar
Jai Menon committed
207
  .decode         = eightsvx_decode_frame,
208
  .capabilities   = CODEC_CAP_DELAY | CODEC_CAP_DR1,
209 210
  .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
                                                    AV_SAMPLE_FMT_NONE },
Jai Menon's avatar
Jai Menon committed
211 212
};

213
AVCodec ff_eightsvx_exp_decoder = {
214
  .name           = "8svx_exp",
215
  .long_name      = NULL_IF_CONFIG_SMALL("8SVX exponential"),
216
  .type           = AVMEDIA_TYPE_AUDIO,
217
  .id             = AV_CODEC_ID_8SVX_EXP,
Jai Menon's avatar
Jai Menon committed
218 219
  .priv_data_size = sizeof (EightSvxContext),
  .init           = eightsvx_decode_init,
220
  .close          = eightsvx_decode_close,
Jai Menon's avatar
Jai Menon committed
221
  .decode         = eightsvx_decode_frame,
222
  .capabilities   = CODEC_CAP_DELAY | CODEC_CAP_DR1,
223 224
  .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
                                                    AV_SAMPLE_FMT_NONE },
Jai Menon's avatar
Jai Menon committed
225
};
226 227 228

AVCodec ff_pcm_s8_planar_decoder = {
    .name           = "pcm_s8_planar",
229
    .long_name      = NULL_IF_CONFIG_SMALL("PCM signed 8-bit planar"),
230
    .type           = AVMEDIA_TYPE_AUDIO,
231
    .id             = AV_CODEC_ID_PCM_S8_PLANAR,
232 233 234 235
    .priv_data_size = sizeof(EightSvxContext),
    .init           = eightsvx_decode_init,
    .close          = eightsvx_decode_close,
    .decode         = eightsvx_decode_frame,
236
    .capabilities   = CODEC_CAP_DELAY | CODEC_CAP_DR1,
237 238
    .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
                                                      AV_SAMPLE_FMT_NONE },
239
};