libavcodec/8svx.c
Go to the documentation of this file.
00001 /*
00002  * 8SVX audio decoder
00003  * Copyright (C) 2008 Jaikrishnan Menon
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00031 #include "avcodec.h"
00032 #include "internal.h"
00033 
00035 typedef struct EightSvxContext {
00036     AVFrame frame;
00037     uint8_t fib_acc[2];
00038     const int8_t *table;
00039 
00040     /* buffer used to store the whole first packet.
00041        data is only sent as one large packet */
00042     uint8_t *data[2];
00043     int data_size;
00044     int data_idx;
00045 } EightSvxContext;
00046 
00047 static const int8_t fibonacci[16]   = { -34, -21, -13,  -8, -5, -3, -2, -1,
00048                                           0,   1,   2,   3,  5,  8, 13, 21 };
00049 static const int8_t exponential[16] = { -128, -64, -32, -16, -8, -4, -2, -1,
00050                                            0,   1,   2,   4,  8, 16, 32, 64 };
00051 
00052 #define MAX_FRAME_SIZE 32768
00053 
00060 static void delta_decode(uint8_t *dst, const uint8_t *src, int src_size,
00061                          uint8_t *state, const int8_t *table, int channels)
00062 {
00063     uint8_t val = *state;
00064 
00065     while (src_size--) {
00066         uint8_t d = *src++;
00067         val = av_clip_uint8(val + table[d & 0xF]);
00068         *dst = val;
00069         dst += channels;
00070         val = av_clip_uint8(val + table[d >> 4]);
00071         *dst = val;
00072         dst += channels;
00073     }
00074 
00075     *state = val;
00076 }
00077 
00078 static void raw_decode(uint8_t *dst, const int8_t *src, int src_size,
00079                        int channels)
00080 {
00081     while (src_size--) {
00082         *dst = *src++ + 128;
00083         dst += channels;
00084     }
00085 }
00086 
00088 static int eightsvx_decode_frame(AVCodecContext *avctx, void *data,
00089                                  int *got_frame_ptr, AVPacket *avpkt)
00090 {
00091     EightSvxContext *esc = avctx->priv_data;
00092     int buf_size;
00093     uint8_t *out_data;
00094     int ret;
00095     int is_compr = (avctx->codec_id != CODEC_ID_PCM_S8_PLANAR);
00096 
00097     /* for the first packet, copy data to buffer */
00098     if (avpkt->data) {
00099         int hdr_size  = is_compr ? 2 : 0;
00100         int chan_size = (avpkt->size - hdr_size * avctx->channels) / avctx->channels;
00101 
00102         if (avpkt->size < hdr_size * avctx->channels) {
00103             av_log(avctx, AV_LOG_ERROR, "packet size is too small\n");
00104             return AVERROR(EINVAL);
00105         }
00106         if (esc->data[0]) {
00107             av_log(avctx, AV_LOG_ERROR, "unexpected data after first packet\n");
00108             return AVERROR(EINVAL);
00109         }
00110 
00111         if (is_compr) {
00112         esc->fib_acc[0] = avpkt->data[1] + 128;
00113         if (avctx->channels == 2)
00114             esc->fib_acc[1] = avpkt->data[2+chan_size+1] + 128;
00115         }
00116 
00117         esc->data_idx  = 0;
00118         esc->data_size = chan_size;
00119         if (!(esc->data[0] = av_malloc(chan_size)))
00120             return AVERROR(ENOMEM);
00121         if (avctx->channels == 2) {
00122             if (!(esc->data[1] = av_malloc(chan_size))) {
00123                 av_freep(&esc->data[0]);
00124                 return AVERROR(ENOMEM);
00125             }
00126         }
00127         memcpy(esc->data[0], &avpkt->data[hdr_size], chan_size);
00128         if (avctx->channels == 2)
00129             memcpy(esc->data[1], &avpkt->data[2*hdr_size+chan_size], chan_size);
00130     }
00131     if (!esc->data[0]) {
00132         av_log(avctx, AV_LOG_ERROR, "unexpected empty packet\n");
00133         return AVERROR(EINVAL);
00134     }
00135 
00136     /* decode next piece of data from the buffer */
00137     buf_size = FFMIN(MAX_FRAME_SIZE, esc->data_size - esc->data_idx);
00138     if (buf_size <= 0) {
00139         *got_frame_ptr = 0;
00140         return avpkt->size;
00141     }
00142 
00143     /* get output buffer */
00144     esc->frame.nb_samples = buf_size * (is_compr + 1);
00145     if ((ret = ff_get_buffer(avctx, &esc->frame)) < 0) {
00146         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00147         return ret;
00148     }
00149     out_data = esc->frame.data[0];
00150 
00151     if (is_compr) {
00152     delta_decode(out_data, &esc->data[0][esc->data_idx], buf_size,
00153                  &esc->fib_acc[0], esc->table, avctx->channels);
00154     if (avctx->channels == 2) {
00155         delta_decode(&out_data[1], &esc->data[1][esc->data_idx], buf_size,
00156                     &esc->fib_acc[1], esc->table, avctx->channels);
00157     }
00158     } else {
00159         int ch;
00160         for (ch = 0; ch < avctx->channels; ch++) {
00161             raw_decode((int8_t *)&out_data[ch], &esc->data[ch][esc->data_idx],
00162                        buf_size, avctx->channels);
00163         }
00164     }
00165     esc->data_idx += buf_size;
00166 
00167     *got_frame_ptr   = 1;
00168     *(AVFrame *)data = esc->frame;
00169 
00170     return avpkt->size;
00171 }
00172 
00174 static av_cold int eightsvx_decode_init(AVCodecContext *avctx)
00175 {
00176     EightSvxContext *esc = avctx->priv_data;
00177 
00178     if (avctx->channels < 1 || avctx->channels > 2) {
00179         av_log(avctx, AV_LOG_ERROR, "8SVX does not support more than 2 channels\n");
00180         return AVERROR(EINVAL);
00181     }
00182 
00183     switch(avctx->codec->id) {
00184         case CODEC_ID_8SVX_FIB:
00185           esc->table = fibonacci;
00186           break;
00187         case CODEC_ID_8SVX_EXP:
00188           esc->table = exponential;
00189           break;
00190         case CODEC_ID_PCM_S8_PLANAR:
00191             break;
00192         default:
00193           return -1;
00194     }
00195     avctx->sample_fmt = AV_SAMPLE_FMT_U8;
00196 
00197     avcodec_get_frame_defaults(&esc->frame);
00198     avctx->coded_frame = &esc->frame;
00199 
00200     return 0;
00201 }
00202 
00203 static av_cold int eightsvx_decode_close(AVCodecContext *avctx)
00204 {
00205     EightSvxContext *esc = avctx->priv_data;
00206 
00207     av_freep(&esc->data[0]);
00208     av_freep(&esc->data[1]);
00209 
00210     return 0;
00211 }
00212 
00213 AVCodec ff_eightsvx_fib_decoder = {
00214   .name           = "8svx_fib",
00215   .type           = AVMEDIA_TYPE_AUDIO,
00216   .id             = CODEC_ID_8SVX_FIB,
00217   .priv_data_size = sizeof (EightSvxContext),
00218   .init           = eightsvx_decode_init,
00219   .close          = eightsvx_decode_close,
00220   .decode         = eightsvx_decode_frame,
00221   .capabilities   = CODEC_CAP_DELAY | CODEC_CAP_DR1,
00222   .long_name      = NULL_IF_CONFIG_SMALL("8SVX fibonacci"),
00223 };
00224 
00225 AVCodec ff_eightsvx_exp_decoder = {
00226   .name           = "8svx_exp",
00227   .type           = AVMEDIA_TYPE_AUDIO,
00228   .id             = CODEC_ID_8SVX_EXP,
00229   .priv_data_size = sizeof (EightSvxContext),
00230   .init           = eightsvx_decode_init,
00231   .close          = eightsvx_decode_close,
00232   .decode         = eightsvx_decode_frame,
00233   .capabilities   = CODEC_CAP_DELAY | CODEC_CAP_DR1,
00234   .long_name      = NULL_IF_CONFIG_SMALL("8SVX exponential"),
00235 };
00236 
00237 AVCodec ff_pcm_s8_planar_decoder = {
00238     .name           = "pcm_s8_planar",
00239     .type           = AVMEDIA_TYPE_AUDIO,
00240     .id             = CODEC_ID_PCM_S8_PLANAR,
00241     .priv_data_size = sizeof(EightSvxContext),
00242     .init           = eightsvx_decode_init,
00243     .close          = eightsvx_decode_close,
00244     .decode         = eightsvx_decode_frame,
00245     .capabilities   = CODEC_CAP_DELAY | CODEC_CAP_DR1,
00246     .long_name      = NULL_IF_CONFIG_SMALL("PCM signed 8-bit planar"),
00247 };