X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;ds=sidebyside;f=libavcodec%2Fws-snd1.c;h=b2d086e07341d51b140a8e9b18d61b3a46403b2e;hb=d1c4ec7f5e9af61067bc1a604e29e465038be63a;hp=c16c99a62a135ab9926c8094bd810e224afcd094;hpb=5d6e4c160a4a0d71c17e8428123027c747ff0fb3;p=ffmpeg diff --git a/libavcodec/ws-snd1.c b/libavcodec/ws-snd1.c index c16c99a62a1..b2d086e0734 100644 --- a/libavcodec/ws-snd1.c +++ b/libavcodec/ws-snd1.c @@ -2,20 +2,20 @@ * Westwood SNDx codecs * Copyright (c) 2005 Konstantin Shishkov * - * This file is part of FFmpeg. + * This file is part of Libav. * - * FFmpeg is free software; you can redistribute it and/or + * Libav is free software; you can redistribute it and/or * 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. * - * FFmpeg is distributed in the hope that it will be useful, + * Libav is distributed in the hope that it will be useful, * 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 - * License along with FFmpeg; if not, write to the Free Software + * License along with Libav; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -25,98 +25,133 @@ /** * @file - * Westwood SNDx codecs. + * Westwood SNDx codecs * * Reference documents about VQA format and its audio codecs * can be found here: * http://www.multimedia.cx */ -static const int8_t ws_adpcm_2bit[] = { -2, -1, 0, 1}; static const int8_t ws_adpcm_4bit[] = { -9, -8, -6, -5, -4, -3, -2, -1, - 0, 1, 2, 3, 4, 5, 6, 8 }; + 0, 1, 2, 3, 4, 5, 6, 8 +}; -#define CLIP8(a) if(a>127)a=127;if(a<-128)a=-128; +typedef struct WSSndContext { + AVFrame frame; +} WSSndContext; -static av_cold int ws_snd_decode_init(AVCodecContext * avctx) +static av_cold int ws_snd_decode_init(AVCodecContext *avctx) { -// WSSNDContext *c = avctx->priv_data; + WSSndContext *s = avctx->priv_data; + + if (avctx->channels != 1) { + av_log_ask_for_sample(avctx, "unsupported number of channels\n"); + return AVERROR(EINVAL); + } + + avctx->sample_fmt = AV_SAMPLE_FMT_U8; + + avcodec_get_frame_defaults(&s->frame); + avctx->coded_frame = &s->frame; - avctx->sample_fmt = AV_SAMPLE_FMT_S16; return 0; } -static int ws_snd_decode_frame(AVCodecContext *avctx, - void *data, int *data_size, - AVPacket *avpkt) +static int ws_snd_decode_frame(AVCodecContext *avctx, void *data, + int *got_frame_ptr, AVPacket *avpkt) { + WSSndContext *s = avctx->priv_data; const uint8_t *buf = avpkt->data; - int buf_size = avpkt->size; -// WSSNDContext *c = avctx->priv_data; + int buf_size = avpkt->size; - int in_size, out_size; - int sample = 0; - int i; - short *samples = data; + int in_size, out_size, ret; + int sample = 128; + uint8_t *samples; + uint8_t *samples_end; if (!buf_size) return 0; + if (buf_size < 4) { + av_log(avctx, AV_LOG_ERROR, "packet is too small\n"); + return AVERROR(EINVAL); + } + out_size = AV_RL16(&buf[0]); - *data_size = out_size * 2; - in_size = AV_RL16(&buf[2]); + in_size = AV_RL16(&buf[2]); buf += 4; - if (out_size > *data_size) { - av_log(avctx, AV_LOG_ERROR, "Frame is too large to fit in buffer\n"); - return -1; - } if (in_size > buf_size) { av_log(avctx, AV_LOG_ERROR, "Frame data is larger than input buffer\n"); return -1; } + + /* get output buffer */ + s->frame.nb_samples = out_size; + if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) { + av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); + return ret; + } + samples = s->frame.data[0]; + samples_end = samples + out_size; + if (in_size == out_size) { - for (i = 0; i < out_size; i++) - *samples++ = (*buf++ - 0x80) << 8; + memcpy(samples, buf, out_size); + *got_frame_ptr = 1; + *(AVFrame *)data = s->frame; return buf_size; } - while (out_size > 0) { - int code; + while (samples < samples_end && buf - avpkt->data < buf_size) { + int code, smp, size; uint8_t count; - code = (*buf) >> 6; - count = (*buf) & 0x3F; + code = *buf >> 6; + count = *buf & 0x3F; buf++; - switch(code) { + + /* make sure we don't write past the output buffer */ + switch (code) { + case 0: smp = 4; break; + case 1: smp = 2; break; + case 2: smp = (count & 0x20) ? 1 : count + 1; break; + default: smp = count + 1; break; + } + if (samples_end - samples < smp) + break; + + /* make sure we don't read past the input buffer */ + size = ((code == 2 && (count & 0x20)) || code == 3) ? 0 : count + 1; + if ((buf - avpkt->data) + size > buf_size) + break; + + switch (code) { case 0: /* ADPCM 2-bit */ for (count++; count > 0; count--) { code = *buf++; - sample += ws_adpcm_2bit[code & 0x3]; - CLIP8(sample); - *samples++ = sample << 8; - sample += ws_adpcm_2bit[(code >> 2) & 0x3]; - CLIP8(sample); - *samples++ = sample << 8; - sample += ws_adpcm_2bit[(code >> 4) & 0x3]; - CLIP8(sample); - *samples++ = sample << 8; - sample += ws_adpcm_2bit[(code >> 6) & 0x3]; - CLIP8(sample); - *samples++ = sample << 8; - out_size -= 4; + sample += ( code & 0x3) - 2; + sample = av_clip_uint8(sample); + *samples++ = sample; + sample += ((code >> 2) & 0x3) - 2; + sample = av_clip_uint8(sample); + *samples++ = sample; + sample += ((code >> 4) & 0x3) - 2; + sample = av_clip_uint8(sample); + *samples++ = sample; + sample += (code >> 6) - 2; + sample = av_clip_uint8(sample); + *samples++ = sample; } break; case 1: /* ADPCM 4-bit */ for (count++; count > 0; count--) { code = *buf++; sample += ws_adpcm_4bit[code & 0xF]; - CLIP8(sample); - *samples++ = sample << 8; + sample = av_clip_uint8(sample); + *samples++ = sample; sample += ws_adpcm_4bit[code >> 4]; - CLIP8(sample); - *samples++ = sample << 8; - out_size -= 2; + sample = av_clip_uint8(sample); + *samples++ = sample; } break; case 2: /* no compression */ @@ -125,35 +160,35 @@ static int ws_snd_decode_frame(AVCodecContext *avctx, t = count; t <<= 3; sample += t >> 3; - *samples++ = sample << 8; - out_size--; + sample = av_clip_uint8(sample); + *samples++ = sample; } else { /* copy */ - for (count++; count > 0; count--) { - *samples++ = (*buf++ - 0x80) << 8; - out_size--; - } - sample = buf[-1] - 0x80; + memcpy(samples, buf, smp); + samples += smp; + buf += smp; + sample = buf[-1]; } break; default: /* run */ - for(count++; count > 0; count--) { - *samples++ = sample << 8; - out_size--; - } + memset(samples, sample, smp); + samples += smp; } } + s->frame.nb_samples = samples - s->frame.data[0]; + *got_frame_ptr = 1; + *(AVFrame *)data = s->frame; + return buf_size; } -AVCodec ws_snd1_decoder = { - "ws_snd1", - AVMEDIA_TYPE_AUDIO, - CODEC_ID_WESTWOOD_SND1, - 0, - ws_snd_decode_init, - NULL, - NULL, - ws_snd_decode_frame, +AVCodec ff_ws_snd1_decoder = { + .name = "ws_snd1", + .type = AVMEDIA_TYPE_AUDIO, + .id = CODEC_ID_WESTWOOD_SND1, + .priv_data_size = sizeof(WSSndContext), + .init = ws_snd_decode_init, + .decode = ws_snd_decode_frame, + .capabilities = CODEC_CAP_DR1, .long_name = NULL_IF_CONFIG_SMALL("Westwood Audio (SND1)"), };