• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavcodec/ws-snd1.c

Go to the documentation of this file.
00001 /*
00002  * Westwood SNDx codecs
00003  * Copyright (c) 2005 Konstantin Shishkov
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 
00022 #include <stdint.h>
00023 #include "libavutil/intreadwrite.h"
00024 #include "avcodec.h"
00025 
00035 static const int8_t ws_adpcm_4bit[] = {
00036     -9, -8, -6, -5, -4, -3, -2, -1,
00037      0,  1,  2,  3,  4,  5,  6,  8
00038 };
00039 
00040 typedef struct WSSndContext {
00041     AVFrame frame;
00042 } WSSndContext;
00043 
00044 static av_cold int ws_snd_decode_init(AVCodecContext *avctx)
00045 {
00046     WSSndContext *s = avctx->priv_data;
00047 
00048     if (avctx->channels != 1) {
00049         av_log_ask_for_sample(avctx, "unsupported number of channels\n");
00050         return AVERROR(EINVAL);
00051     }
00052 
00053     avctx->sample_fmt = AV_SAMPLE_FMT_U8;
00054 
00055     avcodec_get_frame_defaults(&s->frame);
00056     avctx->coded_frame = &s->frame;
00057 
00058     return 0;
00059 }
00060 
00061 static int ws_snd_decode_frame(AVCodecContext *avctx, void *data,
00062                                int *got_frame_ptr, AVPacket *avpkt)
00063 {
00064     WSSndContext *s = avctx->priv_data;
00065     const uint8_t *buf = avpkt->data;
00066     int buf_size       = avpkt->size;
00067 
00068     int in_size, out_size, ret;
00069     int sample = 128;
00070     uint8_t *samples;
00071     uint8_t *samples_end;
00072 
00073     if (!buf_size)
00074         return 0;
00075 
00076     if (buf_size < 4) {
00077         av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
00078         return AVERROR(EINVAL);
00079     }
00080 
00081     out_size = AV_RL16(&buf[0]);
00082     in_size  = AV_RL16(&buf[2]);
00083     buf += 4;
00084 
00085     if (in_size > buf_size) {
00086         av_log(avctx, AV_LOG_ERROR, "Frame data is larger than input buffer\n");
00087         return -1;
00088     }
00089 
00090     /* get output buffer */
00091     s->frame.nb_samples = out_size;
00092     if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
00093         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00094         return ret;
00095     }
00096     samples     = s->frame.data[0];
00097     samples_end = samples + out_size;
00098 
00099     if (in_size == out_size) {
00100         memcpy(samples, buf, out_size);
00101         *got_frame_ptr   = 1;
00102         *(AVFrame *)data = s->frame;
00103         return buf_size;
00104     }
00105 
00106     while (samples < samples_end && buf - avpkt->data < buf_size) {
00107         int code, smp, size;
00108         uint8_t count;
00109         code  = *buf >> 6;
00110         count = *buf & 0x3F;
00111         buf++;
00112 
00113         /* make sure we don't write past the output buffer */
00114         switch (code) {
00115         case 0:  smp = 4*(count+1);                    break;
00116         case 1:  smp = 2*(count+1);                    break;
00117         case 2:  smp = (count & 0x20) ? 1 : count + 1; break;
00118         default: smp = count + 1;                      break;
00119         }
00120         if (samples_end - samples < smp)
00121             break;
00122 
00123         /* make sure we don't read past the input buffer */
00124         size = ((code == 2 && (count & 0x20)) || code == 3) ? 0 : count + 1;
00125         if ((buf - avpkt->data) + size > buf_size)
00126             break;
00127 
00128         switch (code) {
00129         case 0: /* ADPCM 2-bit */
00130             for (count++; count > 0; count--) {
00131                 code = *buf++;
00132                 sample += ( code       & 0x3) - 2;
00133                 sample = av_clip_uint8(sample);
00134                 *samples++ = sample;
00135                 sample += ((code >> 2) & 0x3) - 2;
00136                 sample = av_clip_uint8(sample);
00137                 *samples++ = sample;
00138                 sample += ((code >> 4) & 0x3) - 2;
00139                 sample = av_clip_uint8(sample);
00140                 *samples++ = sample;
00141                 sample +=  (code >> 6)        - 2;
00142                 sample = av_clip_uint8(sample);
00143                 *samples++ = sample;
00144             }
00145             break;
00146         case 1: /* ADPCM 4-bit */
00147             for (count++; count > 0; count--) {
00148                 code = *buf++;
00149                 sample += ws_adpcm_4bit[code & 0xF];
00150                 sample = av_clip_uint8(sample);
00151                 *samples++ = sample;
00152                 sample += ws_adpcm_4bit[code >> 4];
00153                 sample = av_clip_uint8(sample);
00154                 *samples++ = sample;
00155             }
00156             break;
00157         case 2: /* no compression */
00158             if (count & 0x20) { /* big delta */
00159                 int8_t t;
00160                 t = count;
00161                 t <<= 3;
00162                 sample += t >> 3;
00163                 sample = av_clip_uint8(sample);
00164                 *samples++ = sample;
00165             } else { /* copy */
00166                 memcpy(samples, buf, smp);
00167                 samples += smp;
00168                 buf     += smp;
00169                 sample = buf[-1];
00170             }
00171             break;
00172         default: /* run */
00173             memset(samples, sample, smp);
00174             samples += smp;
00175         }
00176     }
00177 
00178     s->frame.nb_samples = samples - s->frame.data[0];
00179     *got_frame_ptr   = 1;
00180     *(AVFrame *)data = s->frame;
00181 
00182     return buf_size;
00183 }
00184 
00185 AVCodec ff_ws_snd1_decoder = {
00186     .name           = "ws_snd1",
00187     .type           = AVMEDIA_TYPE_AUDIO,
00188     .id             = CODEC_ID_WESTWOOD_SND1,
00189     .priv_data_size = sizeof(WSSndContext),
00190     .init           = ws_snd_decode_init,
00191     .decode         = ws_snd_decode_frame,
00192     .capabilities   = CODEC_CAP_DR1,
00193     .long_name = NULL_IF_CONFIG_SMALL("Westwood Audio (SND1)"),
00194 };
Generated on Thu Jan 24 2013 17:08:54 for Libav by doxygen 1.7.1