From b09c7eefb77263b2b1292faa65c292e74c04ff43 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Sun, 11 Sep 2011 11:13:04 -0400 Subject: dpcm: cosmetics: rename channel_number to ch Make the code easier to read. --- libavcodec/dpcm.c | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) (limited to 'libavcodec/dpcm.c') diff --git a/libavcodec/dpcm.c b/libavcodec/dpcm.c index 34c739a790..1c80522a00 100644 --- a/libavcodec/dpcm.c +++ b/libavcodec/dpcm.c @@ -168,7 +168,7 @@ static int dpcm_decode_frame(AVCodecContext *avctx, DPCMContext *s = avctx->priv_data; int in, out = 0; int predictor[2]; - int channel_number = 0; + int ch = 0; short *output_samples = data; int shift[2]; unsigned char byte; @@ -195,12 +195,12 @@ static int dpcm_decode_frame(AVCodecContext *avctx, /* decode the samples */ for (in = 8, out = 0; in < buf_size; in++, out++) { - predictor[channel_number] += s->roq_square_array[buf[in]]; - predictor[channel_number] = av_clip_int16(predictor[channel_number]); - output_samples[out] = predictor[channel_number]; + predictor[ch] += s->roq_square_array[buf[in]]; + predictor[ch] = av_clip_int16(predictor[ch]); + output_samples[out] = predictor[ch]; /* toggle channel */ - channel_number ^= s->channels - 1; + ch ^= s->channels - 1; } break; @@ -218,12 +218,12 @@ static int dpcm_decode_frame(AVCodecContext *avctx, } while (in < buf_size) { - predictor[channel_number] += interplay_delta_table[buf[in++]]; - predictor[channel_number] = av_clip_int16(predictor[channel_number]); - output_samples[out++] = predictor[channel_number]; + predictor[ch] += interplay_delta_table[buf[in++]]; + predictor[ch] = av_clip_int16(predictor[ch]); + output_samples[out++] = predictor[ch]; /* toggle channel */ - channel_number ^= s->channels - 1; + ch ^= s->channels - 1; } break; @@ -244,21 +244,21 @@ static int dpcm_decode_frame(AVCodecContext *avctx, byte = buf[in++]; diff = (byte & 0xFC) << 8; if ((byte & 0x03) == 3) - shift[channel_number]++; + shift[ch]++; else - shift[channel_number] -= (2 * (byte & 3)); + shift[ch] -= (2 * (byte & 3)); /* saturate the shifter to a lower limit of 0 */ - if (shift[channel_number] < 0) - shift[channel_number] = 0; + if (shift[ch] < 0) + shift[ch] = 0; - diff >>= shift[channel_number]; - predictor[channel_number] += diff; + diff >>= shift[ch]; + predictor[ch] += diff; - predictor[channel_number] = av_clip_int16(predictor[channel_number]); - output_samples[out++] = predictor[channel_number]; + predictor[ch] = av_clip_int16(predictor[ch]); + output_samples[out++] = predictor[ch]; /* toggle channel */ - channel_number ^= s->channels - 1; + ch ^= s->channels - 1; } break; case CODEC_ID_SOL_DPCM: @@ -283,12 +283,12 @@ static int dpcm_decode_frame(AVCodecContext *avctx, while (in < buf_size) { int n; n = buf[in++]; - if (n & 0x80) s->sample[channel_number] -= s->sol_table[n & 0x7F]; - else s->sample[channel_number] += s->sol_table[n & 0x7F]; - s->sample[channel_number] = av_clip_int16(s->sample[channel_number]); - output_samples[out++] = s->sample[channel_number]; + if (n & 0x80) s->sample[ch] -= s->sol_table[n & 0x7F]; + else s->sample[ch] += s->sol_table[n & 0x7F]; + s->sample[ch] = av_clip_int16(s->sample[ch]); + output_samples[out++] = s->sample[ch]; /* toggle channel */ - channel_number ^= s->channels - 1; + ch ^= s->channels - 1; } } break; -- cgit v1.2.3 From 3db8db406ff213bf177cc23c44f6cbc8778b1bea Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Sun, 11 Sep 2011 11:19:00 -0400 Subject: dpcm: factor out the stereo flag calculation --- libavcodec/dpcm.c | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) (limited to 'libavcodec/dpcm.c') diff --git a/libavcodec/dpcm.c b/libavcodec/dpcm.c index 1c80522a00..185d04e0b2 100644 --- a/libavcodec/dpcm.c +++ b/libavcodec/dpcm.c @@ -169,6 +169,7 @@ static int dpcm_decode_frame(AVCodecContext *avctx, int in, out = 0; int predictor[2]; int ch = 0; + int stereo = s->channels - 1; short *output_samples = data; int shift[2]; unsigned char byte; @@ -184,11 +185,11 @@ static int dpcm_decode_frame(AVCodecContext *avctx, switch(avctx->codec->id) { case CODEC_ID_ROQ_DPCM: - if (s->channels == 1) - predictor[0] = AV_RL16(&buf[6]); - else { + if (stereo) { predictor[0] = buf[7] << 8; predictor[1] = buf[6] << 8; + } else { + predictor[0] = AV_RL16(&buf[6]); } SE_16BIT(predictor[0]); SE_16BIT(predictor[1]); @@ -200,7 +201,7 @@ static int dpcm_decode_frame(AVCodecContext *avctx, output_samples[out] = predictor[ch]; /* toggle channel */ - ch ^= s->channels - 1; + ch ^= stereo; } break; @@ -210,7 +211,7 @@ static int dpcm_decode_frame(AVCodecContext *avctx, in += 2; SE_16BIT(predictor[0]) output_samples[out++] = predictor[0]; - if (s->channels == 2) { + if (stereo) { predictor[1] = AV_RL16(&buf[in]); in += 2; SE_16BIT(predictor[1]) @@ -223,7 +224,7 @@ static int dpcm_decode_frame(AVCodecContext *avctx, output_samples[out++] = predictor[ch]; /* toggle channel */ - ch ^= s->channels - 1; + ch ^= stereo; } break; @@ -234,7 +235,7 @@ static int dpcm_decode_frame(AVCodecContext *avctx, predictor[0] = AV_RL16(&buf[in]); in += 2; SE_16BIT(predictor[0]); - if (s->channels == 2) { + if (stereo) { predictor[1] = AV_RL16(&buf[in]); in += 2; SE_16BIT(predictor[1]); @@ -258,7 +259,7 @@ static int dpcm_decode_frame(AVCodecContext *avctx, output_samples[out++] = predictor[ch]; /* toggle channel */ - ch ^= s->channels - 1; + ch ^= stereo; } break; case CODEC_ID_SOL_DPCM: @@ -271,13 +272,13 @@ static int dpcm_decode_frame(AVCodecContext *avctx, n1 = (buf[in] >> 4) & 0xF; n2 = buf[in++] & 0xF; s->sample[0] += s->sol_table[n1]; - if (s->sample[0] < 0) s->sample[0] = 0; + if (s->sample[0] < 0) s->sample[0] = 0; if (s->sample[0] > 255) s->sample[0] = 255; output_samples[out++] = (s->sample[0] - 128) << 8; - s->sample[s->channels - 1] += s->sol_table[n2]; - if (s->sample[s->channels - 1] < 0) s->sample[s->channels - 1] = 0; - if (s->sample[s->channels - 1] > 255) s->sample[s->channels - 1] = 255; - output_samples[out++] = (s->sample[s->channels - 1] - 128) << 8; + s->sample[stereo] += s->sol_table[n2]; + if (s->sample[stereo] < 0) s->sample[stereo] = 0; + if (s->sample[stereo] > 255) s->sample[stereo] = 255; + output_samples[out++] = (s->sample[stereo] - 128) << 8; } } else { while (in < buf_size) { @@ -288,7 +289,7 @@ static int dpcm_decode_frame(AVCodecContext *avctx, s->sample[ch] = av_clip_int16(s->sample[ch]); output_samples[out++] = s->sample[ch]; /* toggle channel */ - ch ^= s->channels - 1; + ch ^= stereo; } } break; -- cgit v1.2.3 From 76db17dc7d4f19f9a03bdd6de79c2ea37b76888f Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Sun, 11 Sep 2011 11:51:08 -0400 Subject: dpcm: calculate and check actual output data size prior to decoding. --- libavcodec/dpcm.c | 50 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 16 deletions(-) (limited to 'libavcodec/dpcm.c') diff --git a/libavcodec/dpcm.c b/libavcodec/dpcm.c index 185d04e0b2..3f3b422d96 100644 --- a/libavcodec/dpcm.c +++ b/libavcodec/dpcm.c @@ -178,9 +178,30 @@ static int dpcm_decode_frame(AVCodecContext *avctx, if (!buf_size) return 0; - // almost every DPCM variant expands one byte of data into two - if(*data_size/2 < buf_size) - return -1; + /* calculate output size */ + switch(avctx->codec->id) { + case CODEC_ID_ROQ_DPCM: + out = buf_size - 8; + break; + case CODEC_ID_INTERPLAY_DPCM: + out = buf_size - 6 - s->channels; + break; + case CODEC_ID_XAN_DPCM: + out = buf_size - 2 * s->channels; + break; + case CODEC_ID_SOL_DPCM: + if (avctx->codec_tag != 3) + out = buf_size * 2; + else + out = buf_size; + break; + } + out *= av_get_bytes_per_sample(avctx->sample_fmt); + + if (*data_size < out) { + av_log(avctx, AV_LOG_ERROR, "output buffer is too small\n"); + return AVERROR(EINVAL); + } switch(avctx->codec->id) { @@ -195,10 +216,10 @@ static int dpcm_decode_frame(AVCodecContext *avctx, SE_16BIT(predictor[1]); /* decode the samples */ - for (in = 8, out = 0; in < buf_size; in++, out++) { + for (in = 8; in < buf_size; in++) { predictor[ch] += s->roq_square_array[buf[in]]; predictor[ch] = av_clip_int16(predictor[ch]); - output_samples[out] = predictor[ch]; + *output_samples++ = predictor[ch]; /* toggle channel */ ch ^= stereo; @@ -210,23 +231,22 @@ static int dpcm_decode_frame(AVCodecContext *avctx, predictor[0] = AV_RL16(&buf[in]); in += 2; SE_16BIT(predictor[0]) - output_samples[out++] = predictor[0]; + *output_samples++ = predictor[0]; if (stereo) { predictor[1] = AV_RL16(&buf[in]); in += 2; SE_16BIT(predictor[1]) - output_samples[out++] = predictor[1]; + *output_samples++ = predictor[1]; } while (in < buf_size) { predictor[ch] += interplay_delta_table[buf[in++]]; predictor[ch] = av_clip_int16(predictor[ch]); - output_samples[out++] = predictor[ch]; + *output_samples++ = predictor[ch]; /* toggle channel */ ch ^= stereo; } - break; case CODEC_ID_XAN_DPCM: @@ -256,7 +276,7 @@ static int dpcm_decode_frame(AVCodecContext *avctx, predictor[ch] += diff; predictor[ch] = av_clip_int16(predictor[ch]); - output_samples[out++] = predictor[ch]; + *output_samples++ = predictor[ch]; /* toggle channel */ ch ^= stereo; @@ -265,8 +285,6 @@ static int dpcm_decode_frame(AVCodecContext *avctx, case CODEC_ID_SOL_DPCM: in = 0; if (avctx->codec_tag != 3) { - if(*data_size/4 < buf_size) - return -1; while (in < buf_size) { int n1, n2; n1 = (buf[in] >> 4) & 0xF; @@ -274,11 +292,11 @@ static int dpcm_decode_frame(AVCodecContext *avctx, s->sample[0] += s->sol_table[n1]; if (s->sample[0] < 0) s->sample[0] = 0; if (s->sample[0] > 255) s->sample[0] = 255; - output_samples[out++] = (s->sample[0] - 128) << 8; + *output_samples++ = (s->sample[0] - 128) << 8; s->sample[stereo] += s->sol_table[n2]; if (s->sample[stereo] < 0) s->sample[stereo] = 0; if (s->sample[stereo] > 255) s->sample[stereo] = 255; - output_samples[out++] = (s->sample[stereo] - 128) << 8; + *output_samples++ = (s->sample[stereo] - 128) << 8; } } else { while (in < buf_size) { @@ -287,7 +305,7 @@ static int dpcm_decode_frame(AVCodecContext *avctx, if (n & 0x80) s->sample[ch] -= s->sol_table[n & 0x7F]; else s->sample[ch] += s->sol_table[n & 0x7F]; s->sample[ch] = av_clip_int16(s->sample[ch]); - output_samples[out++] = s->sample[ch]; + *output_samples++ = s->sample[ch]; /* toggle channel */ ch ^= stereo; } @@ -295,7 +313,7 @@ static int dpcm_decode_frame(AVCodecContext *avctx, break; } - *data_size = out * sizeof(short); + *data_size = out; return buf_size; } -- cgit v1.2.3 From 04b24cf94b3582f94a94e28506368d3ee54daad7 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Sun, 11 Sep 2011 12:04:46 -0400 Subject: dpcm: output AV_SAMPLE_FMT_U8 for Sol DPCM subcodecs 1 and 2. Uses the native sample format for the codec instead of left-shifting all samples by 8. --- libavcodec/dpcm.c | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) (limited to 'libavcodec/dpcm.c') diff --git a/libavcodec/dpcm.c b/libavcodec/dpcm.c index 3f3b422d96..ef90ab30e1 100644 --- a/libavcodec/dpcm.c +++ b/libavcodec/dpcm.c @@ -43,7 +43,7 @@ typedef struct DPCMContext { int channels; short roq_square_array[256]; - long sample[2];//for SOL_DPCM + int sample[2]; ///< previous sample (for SOL_DPCM) const int *sol_table;//for SOL_DPCM } DPCMContext; @@ -155,7 +155,11 @@ static av_cold int dpcm_decode_init(AVCodecContext *avctx) break; } - avctx->sample_fmt = AV_SAMPLE_FMT_S16; + if (avctx->codec->id == CODEC_ID_SOL_DPCM && avctx->codec_tag != 3) + avctx->sample_fmt = AV_SAMPLE_FMT_U8; + else + avctx->sample_fmt = AV_SAMPLE_FMT_S16; + return 0; } @@ -285,18 +289,17 @@ static int dpcm_decode_frame(AVCodecContext *avctx, case CODEC_ID_SOL_DPCM: in = 0; if (avctx->codec_tag != 3) { + uint8_t *output_samples_u8 = data; while (in < buf_size) { - int n1, n2; - n1 = (buf[in] >> 4) & 0xF; - n2 = buf[in++] & 0xF; - s->sample[0] += s->sol_table[n1]; - if (s->sample[0] < 0) s->sample[0] = 0; - if (s->sample[0] > 255) s->sample[0] = 255; - *output_samples++ = (s->sample[0] - 128) << 8; - s->sample[stereo] += s->sol_table[n2]; - if (s->sample[stereo] < 0) s->sample[stereo] = 0; - if (s->sample[stereo] > 255) s->sample[stereo] = 255; - *output_samples++ = (s->sample[stereo] - 128) << 8; + uint8_t n = buf[in++]; + + s->sample[0] += s->sol_table[n >> 4]; + s->sample[0] = av_clip_uint8(s->sample[0]); + *output_samples_u8++ = s->sample[0]; + + s->sample[stereo] += s->sol_table[n & 0x0F]; + s->sample[stereo] = av_clip_uint8(s->sample[stereo]); + *output_samples_u8++ = s->sample[stereo]; } } else { while (in < buf_size) { -- cgit v1.2.3 From 989bb7bd0477ef467f374dd8464a88d039f86ebe Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Sun, 11 Sep 2011 12:08:38 -0400 Subject: dpcm: consistently use the variable name 'n' for the next input byte. --- libavcodec/dpcm.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'libavcodec/dpcm.c') diff --git a/libavcodec/dpcm.c b/libavcodec/dpcm.c index ef90ab30e1..a024671fa2 100644 --- a/libavcodec/dpcm.c +++ b/libavcodec/dpcm.c @@ -176,7 +176,6 @@ static int dpcm_decode_frame(AVCodecContext *avctx, int stereo = s->channels - 1; short *output_samples = data; int shift[2]; - unsigned char byte; short diff; if (!buf_size) @@ -266,12 +265,12 @@ static int dpcm_decode_frame(AVCodecContext *avctx, } while (in < buf_size) { - byte = buf[in++]; - diff = (byte & 0xFC) << 8; - if ((byte & 0x03) == 3) + uint8_t n = buf[in++]; + diff = (n & 0xFC) << 8; + if ((n & 0x03) == 3) shift[ch]++; else - shift[ch] -= (2 * (byte & 3)); + shift[ch] -= (2 * (n & 3)); /* saturate the shifter to a lower limit of 0 */ if (shift[ch] < 0) shift[ch] = 0; @@ -303,8 +302,7 @@ static int dpcm_decode_frame(AVCodecContext *avctx, } } else { while (in < buf_size) { - int n; - n = buf[in++]; + uint8_t n = buf[in++]; if (n & 0x80) s->sample[ch] -= s->sol_table[n & 0x7F]; else s->sample[ch] += s->sol_table[n & 0x7F]; s->sample[ch] = av_clip_int16(s->sample[ch]); -- cgit v1.2.3 From fc6faee0d1d8bfea642be462c7fd6dcc964f6110 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Sun, 11 Sep 2011 12:13:13 -0400 Subject: dpcm: move codec-specific variable declarations to their corresponding decoding blocks. --- libavcodec/dpcm.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'libavcodec/dpcm.c') diff --git a/libavcodec/dpcm.c b/libavcodec/dpcm.c index a024671fa2..02267d7641 100644 --- a/libavcodec/dpcm.c +++ b/libavcodec/dpcm.c @@ -175,8 +175,6 @@ static int dpcm_decode_frame(AVCodecContext *avctx, int ch = 0; int stereo = s->channels - 1; short *output_samples = data; - int shift[2]; - short diff; if (!buf_size) return 0; @@ -253,8 +251,9 @@ static int dpcm_decode_frame(AVCodecContext *avctx, break; case CODEC_ID_XAN_DPCM: + { + int shift[2] = { 4, 4 }; in = 0; - shift[0] = shift[1] = 4; predictor[0] = AV_RL16(&buf[in]); in += 2; SE_16BIT(predictor[0]); @@ -266,7 +265,7 @@ static int dpcm_decode_frame(AVCodecContext *avctx, while (in < buf_size) { uint8_t n = buf[in++]; - diff = (n & 0xFC) << 8; + int16_t diff = (n & 0xFC) << 8; if ((n & 0x03) == 3) shift[ch]++; else @@ -285,6 +284,7 @@ static int dpcm_decode_frame(AVCodecContext *avctx, ch ^= stereo; } break; + } case CODEC_ID_SOL_DPCM: in = 0; if (avctx->codec_tag != 3) { -- cgit v1.2.3 From 1de8401c569b55abc86b2a06f35c5b3fc72c98ed Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Sun, 11 Sep 2011 12:24:03 -0400 Subject: dpcm: remove unnecessary variable by using bytestream functions. Uses 'buf' directly instead of a separate iterator variable 'in'. --- libavcodec/dpcm.c | 66 +++++++++++++++++++++++-------------------------------- 1 file changed, 28 insertions(+), 38 deletions(-) (limited to 'libavcodec/dpcm.c') diff --git a/libavcodec/dpcm.c b/libavcodec/dpcm.c index 02267d7641..57201fd1c0 100644 --- a/libavcodec/dpcm.c +++ b/libavcodec/dpcm.c @@ -39,6 +39,7 @@ #include "libavutil/intreadwrite.h" #include "avcodec.h" +#include "bytestream.h" typedef struct DPCMContext { int channels; @@ -47,8 +48,6 @@ typedef struct DPCMContext { const int *sol_table;//for SOL_DPCM } DPCMContext; -#define SE_16BIT(x) if (x & 0x8000) x -= 0x10000; - static const int interplay_delta_table[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, @@ -169,8 +168,9 @@ static int dpcm_decode_frame(AVCodecContext *avctx, { const uint8_t *buf = avpkt->data; int buf_size = avpkt->size; + const uint8_t *buf_end = buf + buf_size; DPCMContext *s = avctx->priv_data; - int in, out = 0; + int out = 0; int predictor[2]; int ch = 0; int stereo = s->channels - 1; @@ -207,18 +207,18 @@ static int dpcm_decode_frame(AVCodecContext *avctx, switch(avctx->codec->id) { case CODEC_ID_ROQ_DPCM: + buf += 6; + if (stereo) { - predictor[0] = buf[7] << 8; - predictor[1] = buf[6] << 8; + predictor[1] = (int16_t)(bytestream_get_byte(&buf) << 8); + predictor[0] = (int16_t)(bytestream_get_byte(&buf) << 8); } else { - predictor[0] = AV_RL16(&buf[6]); + predictor[0] = (int16_t)bytestream_get_le16(&buf); } - SE_16BIT(predictor[0]); - SE_16BIT(predictor[1]); /* decode the samples */ - for (in = 8; in < buf_size; in++) { - predictor[ch] += s->roq_square_array[buf[in]]; + while (buf < buf_end) { + predictor[ch] += s->roq_square_array[*buf++]; predictor[ch] = av_clip_int16(predictor[ch]); *output_samples++ = predictor[ch]; @@ -228,20 +228,16 @@ static int dpcm_decode_frame(AVCodecContext *avctx, break; case CODEC_ID_INTERPLAY_DPCM: - in = 6; /* skip over the stream mask and stream length */ - predictor[0] = AV_RL16(&buf[in]); - in += 2; - SE_16BIT(predictor[0]) - *output_samples++ = predictor[0]; - if (stereo) { - predictor[1] = AV_RL16(&buf[in]); - in += 2; - SE_16BIT(predictor[1]) - *output_samples++ = predictor[1]; + buf += 6; /* skip over the stream mask and stream length */ + + for (ch = 0; ch < s->channels; ch++) { + predictor[ch] = (int16_t)bytestream_get_le16(&buf); + *output_samples++ = predictor[ch]; } - while (in < buf_size) { - predictor[ch] += interplay_delta_table[buf[in++]]; + ch = 0; + while (buf < buf_end) { + predictor[ch] += interplay_delta_table[*buf++]; predictor[ch] = av_clip_int16(predictor[ch]); *output_samples++ = predictor[ch]; @@ -253,18 +249,13 @@ static int dpcm_decode_frame(AVCodecContext *avctx, case CODEC_ID_XAN_DPCM: { int shift[2] = { 4, 4 }; - in = 0; - predictor[0] = AV_RL16(&buf[in]); - in += 2; - SE_16BIT(predictor[0]); - if (stereo) { - predictor[1] = AV_RL16(&buf[in]); - in += 2; - SE_16BIT(predictor[1]); - } - while (in < buf_size) { - uint8_t n = buf[in++]; + for (ch = 0; ch < s->channels; ch++) + predictor[ch] = (int16_t)bytestream_get_le16(&buf); + + ch = 0; + while (buf < buf_end) { + uint8_t n = *buf++; int16_t diff = (n & 0xFC) << 8; if ((n & 0x03) == 3) shift[ch]++; @@ -286,11 +277,10 @@ static int dpcm_decode_frame(AVCodecContext *avctx, break; } case CODEC_ID_SOL_DPCM: - in = 0; if (avctx->codec_tag != 3) { uint8_t *output_samples_u8 = data; - while (in < buf_size) { - uint8_t n = buf[in++]; + while (buf < buf_end) { + uint8_t n = *buf++; s->sample[0] += s->sol_table[n >> 4]; s->sample[0] = av_clip_uint8(s->sample[0]); @@ -301,8 +291,8 @@ static int dpcm_decode_frame(AVCodecContext *avctx, *output_samples_u8++ = s->sample[stereo]; } } else { - while (in < buf_size) { - uint8_t n = buf[in++]; + while (buf < buf_end) { + uint8_t n = *buf++; if (n & 0x80) s->sample[ch] -= s->sol_table[n & 0x7F]; else s->sample[ch] += s->sol_table[n & 0x7F]; s->sample[ch] = av_clip_int16(s->sample[ch]); -- cgit v1.2.3 From 4bad464e7f5731e2f554ea673f54e8d770bc109e Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Sun, 11 Sep 2011 12:37:17 -0400 Subject: dpcm: misc pretty-printing --- libavcodec/dpcm.c | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) (limited to 'libavcodec/dpcm.c') diff --git a/libavcodec/dpcm.c b/libavcodec/dpcm.c index 57201fd1c0..cb128affed 100644 --- a/libavcodec/dpcm.c +++ b/libavcodec/dpcm.c @@ -84,13 +84,15 @@ static const int interplay_delta_table[] = { }; -static const int sol_table_old[16] = - { 0x0, 0x1, 0x2 , 0x3, 0x6, 0xA, 0xF, 0x15, - -0x15, -0xF, -0xA, -0x6, -0x3, -0x2, -0x1, 0x0}; +static const int sol_table_old[16] = { + 0x0, 0x1, 0x2, 0x3, 0x6, 0xA, 0xF, 0x15, + -0x15, -0xF, -0xA, -0x6, -0x3, -0x2, -0x1, 0x0 +}; -static const int sol_table_new[16] = - { 0x0, 0x1, 0x2, 0x3, 0x6, 0xA, 0xF, 0x15, - 0x0, -0x1, -0x2, -0x3, -0x6, -0xA, -0xF, -0x15}; +static const int sol_table_new[16] = { + 0x0, 0x1, 0x2, 0x3, 0x6, 0xA, 0xF, 0x15, + 0x0, -0x1, -0x2, -0x3, -0x6, -0xA, -0xF, -0x15 +}; static const int sol_table_16[128] = { 0x000, 0x008, 0x010, 0x020, 0x030, 0x040, 0x050, 0x060, 0x070, 0x080, @@ -109,7 +111,6 @@ static const int sol_table_16[128] = { }; - static av_cold int dpcm_decode_init(AVCodecContext *avctx) { DPCMContext *s = avctx->priv_data; @@ -125,24 +126,23 @@ static av_cold int dpcm_decode_init(AVCodecContext *avctx) /* initialize square table */ for (i = 0; i < 128; i++) { square = i * i; - s->roq_square_array[i] = square; + s->roq_square_array[i ] = square; s->roq_square_array[i + 128] = -square; } break; - case CODEC_ID_SOL_DPCM: switch(avctx->codec_tag){ case 1: - s->sol_table=sol_table_old; + s->sol_table = sol_table_old; s->sample[0] = s->sample[1] = 0x80; break; case 2: - s->sol_table=sol_table_new; + s->sol_table = sol_table_new; s->sample[0] = s->sample[1] = 0x80; break; case 3: - s->sol_table=sol_table_16; + s->sol_table = sol_table_16; break; default: av_log(avctx, AV_LOG_ERROR, "Unknown SOL subcodec\n"); @@ -162,8 +162,8 @@ static av_cold int dpcm_decode_init(AVCodecContext *avctx) return 0; } -static int dpcm_decode_frame(AVCodecContext *avctx, - void *data, int *data_size, + +static int dpcm_decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt) { const uint8_t *buf = avpkt->data; @@ -219,7 +219,7 @@ static int dpcm_decode_frame(AVCodecContext *avctx, /* decode the samples */ while (buf < buf_end) { predictor[ch] += s->roq_square_array[*buf++]; - predictor[ch] = av_clip_int16(predictor[ch]); + predictor[ch] = av_clip_int16(predictor[ch]); *output_samples++ = predictor[ch]; /* toggle channel */ @@ -238,7 +238,7 @@ static int dpcm_decode_frame(AVCodecContext *avctx, ch = 0; while (buf < buf_end) { predictor[ch] += interplay_delta_table[*buf++]; - predictor[ch] = av_clip_int16(predictor[ch]); + predictor[ch] = av_clip_int16(predictor[ch]); *output_samples++ = predictor[ch]; /* toggle channel */ @@ -294,7 +294,7 @@ static int dpcm_decode_frame(AVCodecContext *avctx, while (buf < buf_end) { uint8_t n = *buf++; if (n & 0x80) s->sample[ch] -= s->sol_table[n & 0x7F]; - else s->sample[ch] += s->sol_table[n & 0x7F]; + else s->sample[ch] += s->sol_table[n & 0x7F]; s->sample[ch] = av_clip_int16(s->sample[ch]); *output_samples++ = s->sample[ch]; /* toggle channel */ @@ -320,6 +320,6 @@ AVCodec ff_ ## name_ ## _decoder = { \ } DPCM_DECODER(CODEC_ID_INTERPLAY_DPCM, interplay_dpcm, "DPCM Interplay"); -DPCM_DECODER(CODEC_ID_ROQ_DPCM, roq_dpcm, "DPCM id RoQ"); -DPCM_DECODER(CODEC_ID_SOL_DPCM, sol_dpcm, "DPCM Sol"); -DPCM_DECODER(CODEC_ID_XAN_DPCM, xan_dpcm, "DPCM Xan"); +DPCM_DECODER(CODEC_ID_ROQ_DPCM, roq_dpcm, "DPCM id RoQ"); +DPCM_DECODER(CODEC_ID_SOL_DPCM, sol_dpcm, "DPCM Sol"); +DPCM_DECODER(CODEC_ID_XAN_DPCM, xan_dpcm, "DPCM Xan"); -- cgit v1.2.3 From 8d77d12a2be10e8564599ccbfcdd804ff432d885 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Sun, 11 Sep 2011 12:40:40 -0400 Subject: dpcm: check to make sure channels is 1 or 2. --- libavcodec/dpcm.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'libavcodec/dpcm.c') diff --git a/libavcodec/dpcm.c b/libavcodec/dpcm.c index cb128affed..fc38abdb7a 100644 --- a/libavcodec/dpcm.c +++ b/libavcodec/dpcm.c @@ -117,6 +117,11 @@ static av_cold int dpcm_decode_init(AVCodecContext *avctx) int i; short square; + if (avctx->channels < 1 || avctx->channels > 2) { + av_log(avctx, AV_LOG_INFO, "invalid number of channels\n"); + return AVERROR(EINVAL); + } + s->channels = avctx->channels; s->sample[0] = s->sample[1] = 0; -- cgit v1.2.3 From f47f7efd14fa57e298886a20920b7679c975afc1 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Sun, 11 Sep 2011 12:41:48 -0400 Subject: dpcm: replace short with int16_t --- libavcodec/dpcm.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'libavcodec/dpcm.c') diff --git a/libavcodec/dpcm.c b/libavcodec/dpcm.c index fc38abdb7a..3ddc676a72 100644 --- a/libavcodec/dpcm.c +++ b/libavcodec/dpcm.c @@ -43,7 +43,7 @@ typedef struct DPCMContext { int channels; - short roq_square_array[256]; + int16_t roq_square_array[256]; int sample[2]; ///< previous sample (for SOL_DPCM) const int *sol_table;//for SOL_DPCM } DPCMContext; @@ -115,7 +115,6 @@ static av_cold int dpcm_decode_init(AVCodecContext *avctx) { DPCMContext *s = avctx->priv_data; int i; - short square; if (avctx->channels < 1 || avctx->channels > 2) { av_log(avctx, AV_LOG_INFO, "invalid number of channels\n"); @@ -130,7 +129,7 @@ static av_cold int dpcm_decode_init(AVCodecContext *avctx) case CODEC_ID_ROQ_DPCM: /* initialize square table */ for (i = 0; i < 128; i++) { - square = i * i; + int16_t square = i * i; s->roq_square_array[i ] = square; s->roq_square_array[i + 128] = -square; } @@ -179,7 +178,7 @@ static int dpcm_decode_frame(AVCodecContext *avctx, void *data, int *data_size, int predictor[2]; int ch = 0; int stereo = s->channels - 1; - short *output_samples = data; + int16_t *output_samples = data; if (!buf_size) return 0; -- cgit v1.2.3 From 5a54d5101780421cdcb71c018eea157e3d4d3c8f Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Sun, 11 Sep 2011 12:43:03 -0400 Subject: dpcm: use sol_table_16 directly instead of through the DPCMContext. --- libavcodec/dpcm.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'libavcodec/dpcm.c') diff --git a/libavcodec/dpcm.c b/libavcodec/dpcm.c index 3ddc676a72..2491292984 100644 --- a/libavcodec/dpcm.c +++ b/libavcodec/dpcm.c @@ -146,7 +146,6 @@ static av_cold int dpcm_decode_init(AVCodecContext *avctx) s->sample[0] = s->sample[1] = 0x80; break; case 3: - s->sol_table = sol_table_16; break; default: av_log(avctx, AV_LOG_ERROR, "Unknown SOL subcodec\n"); @@ -297,8 +296,8 @@ static int dpcm_decode_frame(AVCodecContext *avctx, void *data, int *data_size, } else { while (buf < buf_end) { uint8_t n = *buf++; - if (n & 0x80) s->sample[ch] -= s->sol_table[n & 0x7F]; - else s->sample[ch] += s->sol_table[n & 0x7F]; + if (n & 0x80) s->sample[ch] -= sol_table_16[n & 0x7F]; + else s->sample[ch] += sol_table_16[n & 0x7F]; s->sample[ch] = av_clip_int16(s->sample[ch]); *output_samples++ = s->sample[ch]; /* toggle channel */ -- cgit v1.2.3 From 0354fb7ebe177773e44b889ba7672e4336ee377f Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Sun, 11 Sep 2011 12:46:06 -0400 Subject: dpcm: use smaller data types for static tables --- libavcodec/dpcm.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'libavcodec/dpcm.c') diff --git a/libavcodec/dpcm.c b/libavcodec/dpcm.c index 2491292984..d4cf806b82 100644 --- a/libavcodec/dpcm.c +++ b/libavcodec/dpcm.c @@ -45,10 +45,10 @@ typedef struct DPCMContext { int channels; int16_t roq_square_array[256]; int sample[2]; ///< previous sample (for SOL_DPCM) - const int *sol_table;//for SOL_DPCM + const int8_t *sol_table; ///< delta table for SOL_DPCM } DPCMContext; -static const int interplay_delta_table[] = { +static const int16_t interplay_delta_table[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, @@ -84,17 +84,17 @@ static const int interplay_delta_table[] = { }; -static const int sol_table_old[16] = { +static const int8_t sol_table_old[16] = { 0x0, 0x1, 0x2, 0x3, 0x6, 0xA, 0xF, 0x15, -0x15, -0xF, -0xA, -0x6, -0x3, -0x2, -0x1, 0x0 }; -static const int sol_table_new[16] = { +static const int8_t sol_table_new[16] = { 0x0, 0x1, 0x2, 0x3, 0x6, 0xA, 0xF, 0x15, 0x0, -0x1, -0x2, -0x3, -0x6, -0xA, -0xF, -0x15 }; -static const int sol_table_16[128] = { +static const int16_t sol_table_16[128] = { 0x000, 0x008, 0x010, 0x020, 0x030, 0x040, 0x050, 0x060, 0x070, 0x080, 0x090, 0x0A0, 0x0B0, 0x0C0, 0x0D0, 0x0E0, 0x0F0, 0x100, 0x110, 0x120, 0x130, 0x140, 0x150, 0x160, 0x170, 0x180, 0x190, 0x1A0, 0x1B0, 0x1C0, -- cgit v1.2.3 From 08bd22a61b820160bff5f98cd51d2e0135d02e00 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Sun, 11 Sep 2011 13:18:51 -0400 Subject: dpcm: return error if packet is too small --- libavcodec/dpcm.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'libavcodec/dpcm.c') diff --git a/libavcodec/dpcm.c b/libavcodec/dpcm.c index d4cf806b82..6ba8ab13ce 100644 --- a/libavcodec/dpcm.c +++ b/libavcodec/dpcm.c @@ -201,7 +201,10 @@ static int dpcm_decode_frame(AVCodecContext *avctx, void *data, int *data_size, break; } out *= av_get_bytes_per_sample(avctx->sample_fmt); - + if (out < 0) { + av_log(avctx, AV_LOG_ERROR, "packet is too small\n"); + return AVERROR(EINVAL); + } if (*data_size < out) { av_log(avctx, AV_LOG_ERROR, "output buffer is too small\n"); return AVERROR(EINVAL); -- cgit v1.2.3